This C program to find the size of file accept the name of file and display the size of file by using file handling build in function fseek() and ftell().First it open the file in read mode and then apply the above function to calculate the size of file.
C Program to Find the Size of File using File Handling Function
void main(int argc, char **argv)
{
FILE *fp;
char ch;
int size = 0;
fp = fopen(argv[1], "r");
if (fp == NULL)
printf("\nFile unable to open ");
else
printf("\nFile opened ");
fseek(fp, 0, 2); /* file pointer at the end of file */
size = ftell(fp); /* take a position of file pointer un size variable */
printf("The size of given file is : %d\n", size);
fclose(fp);
}
Output
File opened The size of given file is : 79174
The above output may be display on the output screen if file is exist in the system.