C Program to find the size of a File
Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given statements.
C: C is a programming language developed at AT and T in bell laboratories by Dennis Ritchie, it is a system programming language and procedure-oriented programming language, and it is known as a mid-level programming language.
Features of C Language
- Portability
- Easy to extend
- Fast and efficient
- Simple and faster
- Rich in library
The size of a File concept is part of the C Programming language, which we will discuss below:
What is the file size?
In our daily life or the life of software, we come across many files which may be of different formats and different sizes. We also use a zip file where we compress the file to create this. To know the content in the zip file, we need to extract the file, and then we need to import the file into our laptop. File size refers to how much information or data is in the system file.
File sizes may be of different sizes. For example, it can be expressed in bytes, bits, kilobytes (KB), megabytes (MB), gigabytes (GB), etc. In the mentioned above sizes, a gigabyte is the highest file size, and we are going to have many other which are greater than gigabytes. The picture shown below is very familiar to us in our systems.

1) Program to find file size
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
int n;
fp=fopen("demo.txt"r");
if(fp==NULL)
{
printf("\nError cannot open file");
exit(1);
}
fseek (fp,0, SEEK_END);
n=ftell(fp);
printf("\nSize of the file = %d", n) ;
fclose(fp);
}
Output:
Size of the file = 23
Explanation of above Program
To get the file size, we commonly use certain functions like fseek(), ftell(), fopen(), and fclose(). This will perform an individual task.
fseek(): This function is used to move or transfer file pointer, which is linked with the present file you have created to a particular position or destination.
ftell(): This function is used to find the total file size after the file pointer is moved to the particular position
fclose(): This function is used to close the stream. Used at the last of the Program to exit from the stream.
fopen(): This function is used to open a file and perform a specific task or operation, which consists of writing and reading, which are represented with the parameters w and r.
These functions come in libraries that need to be imported from the standard library, which is indicated as #include<stdio.h>.
2) Program to find file size
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch;
int size = 0;
fp = fopen("demo.txt", "r");
if (fp == NULL)
{
printf("\nunable to open the file");
}
else
{
printf("\nopened the file");
}
fseek(fp, 0, SEEK_END);
size = ftell(fp);
printf("\nthe opened file size is %d\n", size);
fclose(fp);
}
Output:
Opened the file
The opened file size is 23
Explanation of above Program
In the above Program, we will open the file in a reading mode which lets us know the size after opening, and we cannot edit the text file. If you want to edit the text, we need to open the file using writing mode, which is indicated with the parameter 'w', and reading mode is indicated with the parameter 'r'. Now we are going to use conditional statements in which we will specify if fp = NULL, we are going to print unable to open the file. Else, it will print the opened file. When fopen() is unable to open a file, then it will display NULL, then this word is being used in the conditional statements if and else.
Compact Program to find the Size of a File:
#include<stdio.h>
int main()
{
FILE *fp;
long int p;
fp=fopen("demo.txt", "r");
fseek(fp,0,SEEK_END);
p=ftell(fp);
printf("the opened file size is : %ld bytes.",p);
return 0;
}
Output:
The opened file size is: 23
Note: In my case file size is 23, but it may vary for others.
Explanation of above Program
The above Program is the simple one. In this Program, we have not used conditional statements or any other complicated statements that could raise an error.
We used 3 functions, fopen, fseek, and ftell, in which one will open the file, another will change the destination of the file pointer, and another will tell the file size. This is the process involved in finding the size of the file.
Note: In the fopen function in the above all programs, we give "demo.text" because we need to find the size of that file, and that file should exist in your system or computer.
If the file does not open, it will display an error or other information on the terminal.
This is all about the size of the file in c programming language and their programs.