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.

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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.