Are you new to C programming and searching for a way to find the size of a file? Go nowhere else! In this guide, you will learn the process of finding the size of a file in C programming. The number of bytes utilized to hold the data inside a file is referred to as the file’s size. By knowing file size, you can perform various file-handling operations, including copying, reading, or writing.
Through the step-by-step guidelines mentioned in this tutorial and simple code examples, you can quickly find the size of any file.
Find the Size of a File in C Programming
There are different ways to find the size of a file in C mentioned below.
1: Using stat() Function
In C, the most straightforward method of finding out the size of a file is to use the stat() system call function. The stat() function retrieves file status information and stores it in a struct. One of the members of this struct is st_size, which gives the size of the file in bytes. This method avoids having to manually move the file pointer, making it easier to use and less error-prone.
#include <sys/stat.h>
int main()
{
struct stat st;
long size;
if (stat("C_File.txt", &st) == 0)
{
size = st.st_size;
printf("The size of the file is %ld bytes.\n", size);
}
return 0;
}
This code retrieves the size of a file called C_File.txt using the stat() method from the <sys/stat.h> library. The st_size element of the struct stat, in which the function keeps the information about the file, and allows us to find the file size. The size of the file is kept in the variable size, and then printf() is used to print the size to the console along with a message. Before attempting to access the st_size property, the if statement verifies that the stat() function call was successful. The code then returns 0 to show that it ran successfully.
Output
2: Using fstat() Function
Another function similar to stat() is the fstat() function, which is equivalent to stat() but works on an open file instead of a filename. The fstat() function retrieves information about the open file described by the file descriptor passed to it. A reference to an open file is represented by the integer file descriptor. Similarly to stat(), fstat() returns the status information about the file provided, including its size in bytes.
#include <sys/stat.h>
#include <fcntl.h>
int main() {
int fd;
struct stat st;
fd = open("C_File.txt", O_RDONLY);
if (fstat(fd, &st) == -1) {
printf("Error getting file information.\n");
return 1;
}
printf("The size of the file is %ld bytes.\n", st.st_size);
close(fd);
return 0;
}
In the above code, the file is initially opened in this example by calling the open() function with the filename and the O_RDONLY option, which opens the file in read-only mode. A file descriptor (fd) is returned by the function. After that, we use the fstat() method to obtain information about the file, sending it as a pointer to the st structure as the second parameter and the file descriptor as the first argument. The st structure is where this function keeps the file’s information after retrieving it.
The size of the file in bytes, which is kept in the st_size member of the st structure, is then printed if there was no error while using the fstat() function. Otherwise, it prints an error message.
Output
3: Using fseek() and ftell() Functions
One other method to find the size of a file in C programming is to use the fseek() and ftell() functions. The ftell() method returns the current position of the file pointer, whereas the fseek() function moves the file pointer to a given place in the file. The file’s size in bytes may be found by using the ftell() function to acquire the current position after using the fseek() function to relocate the file pointer to the end of the file.
#include <stdlib.h>
int main()
{
FILE *fp;
long size;
fp = fopen("C_File.txt", "rb");
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fclose(fp);
printf("The size of the file = %ld bytes.\n", size);
return 0;
}
In the above code, using the fopen() method, the application first opens the C_File.txt in binary mode. The fseek() function then uses the SEEK_END option to relocate the file pointer to the file’s end. The file’s current location, which corresponds to its size in bytes, may then be found using the ftell() function. The code then uses the fclose() method to close the file after printing the file’s size to the console.
Output
4: Using filelength() Function
The next method is by using the file management library, which provides many file manipulation functions, including functions to get the size of a file. For example, the function filelength() returns the length of a file in bytes, given its filename. This function requires the header file <io.h>.
There is also another function, _filelengthi64(), that returns the length of a file in bytes, for files larger than 4 GB. The difference between the two functions is the type of their return values, which shows how many bytes the function can handle.
#include <io.h>
int main()
{
long size;
char* fname = "C_File.txt";
size = filelength(fileno(fopen(fname, "rb")));
printf("The size of the file = %ld bytes.\n", size);
return 0;
}
The size of the file C_File.txt in bytes is obtained by this code using the Windows file management library. When provided the file descriptor of an opened file, the method filelength() returns the length of the file in bytes.
fopen() opens the file in read-only binary mode in this code, and fileno() returns the file descriptor of the opened file. Then, printf() is used to print the file size. The function then returns 0 which indicates that it has been successfully executed.
Output
Conclusion
There are several methods to find the size of a file in bytes in C, each with its own advantages and disadvantages. Four different methods are discussed in the tutorial above; stat() function, fstat() function, fseek() and ftell() functions, and the filelength() function. Understanding these functions will help users find the size of any file on your system.