Monday 29 May 2017

Find Size of a File ~ GNIITHELP

Program to Find Size of a File

We will be using fseek() and ftell() functions to find the size of the file. There are others ways to find the file size, like looping on the whole content of file and finding out the size, but using File Handling functions makes it easier.
#include<stdio.h>
#include<conio.h>

void main()
{
    FILE *fp;
    char ch;
    int size = 0;
 
    fp = fopen("MyFile.txt", "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);
}
To learn about File Handling in C, Visit the studytonight Lesson : File Handling in C

No comments:

Post a Comment