Feb 27, 2012

C Program to find the size of a given file.

Write a C Program to find the size of a given file.
The function fseek() is used to set the file pointer at the specified position. In general, it is used to move the file pointer to the desired position within the file.
In this program we moved the file pointer to the end of file, and obtain the current position of the file pointer. Function rewind() also moves the file pointer to the beginning of the file, but syntax and number of parameters are different. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/


#include<stdio.h>
#include<process.h>
long file_size(FILE *fp)
 {
     long len;
    //move file pointer to end of the file */
    fseek(fp, 0L, 2);
    //Obtain the current position of file pointer
    len = ftell(fp);
    return len;
 }
void main(void)
 {
   FILE *fp;
   char filename[20];
   printf("\nEnter the file name:\n\n");
   scanf("%s",filename);

   fp = fopen(filename, r);
   
   if(fp == NULL)
    {
       printf("Error:  file not found!\n\n");
       exit(0);
    }
  printf("File size of %s is %ld bytes\n\n",filename, file_size(fp));
  fclose(fp);
 }

Read more Similar C Programs
C Basic

C File i/o
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link