C Program to Find the File Size using File Handling Function

«
»
This C Program finds the size of file using file handling function.

Here is source code of the C Program to find the size of file using file handling function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Find the Size of File using File Handling Function
  3.  */
  4. #include <stdio.h>
  5.  
  6. void main(int argc, char **argv)
  7. {
  8.     FILE *fp;
  9.     char ch;
  10.     int size = 0;
  11.  
  12.     fp = fopen(argv[1], "r");
  13.     if (fp == NULL)
  14.         printf("\nFile unable to open ");
  15.     else 
  16.         printf("\nFile opened ");
  17.     fseek(fp, 0, 2);    /* file pointer at the end of file */
  18.     size = ftell(fp);   /* take a position of file pointer un size variable */
  19.     printf("The size of given file is : %d\n", size);    
  20.     fclose(fp);
  21. }

$ vi file10.c
$ cc file10.c
$ a.out myvmlinux
 
File opened The size of given file is : 3791744

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.