When it comes to programming in C, understanding file size can be crucial. Whether you are working on a small project or a large-scale application, knowing how to determine the file size can be extremely useful.
In this comprehensive guide, we will explore the various methods for determining file size in C. From simple approaches to more advanced techniques, you will have a complete understanding of how to get the file size in your C programs.
Method 1: Using the sizeof() Function
The sizeof() function is a built-in function in C that returns the size of a variable or data type in bytes. This is a simple and straightforward approach to determine the file size in C.
Let's say we have a file named "example.txt" and we want to get its size in bytes. We can use the following code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char filename[] = "example.txt";
long size;
fp = fopen(filename,"rb"); // open file in read mode
if (fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
// get the size using the fseek() and ftell() functions
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
printf("Size of %s = %ld bytes\n", filename, size);
fclose(fp);
return 0;
}
In this code, we first open the file in read mode using the fopen() function. Then, we use fseek() to move the file pointer to the end of the file and use ftell() to get the current position of the file pointer, which is the size of the file. Finally, we print the size in bytes and close the file.
Method 2: Using the stat() Function
The stat() function is another way to get the file size in C. It is a part of the sys/stat.h header file and returns information about a file, including its size.
Let's take a look at how we can use the stat() function to get the file size:
#include <stdio.h>
#include <sys/stat.h>
int main()
{
struct stat st;
char filename[] = "example.txt";
if (stat(filename, &st) == 0)
{
printf("Size of %s = %ld bytes\n", filename, st.st_size);
}
else
{
printf("Error getting file size\n");
}
return 0;
}
In this code, we use the stat() function and pass the file name and a pointer to a struct stat variable as arguments. The size of the file is then available in the st_size field of the struct.
Method 3: Using the fseek() and ftell() Functions
As we saw in method 1, we can use the fseek() and ftell() functions to get the file size. However, there is another way to use these functions to get the file size in C.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char filename[] = "example.txt";
long size;
fp = fopen(filename,"rb"); // open file in read mode
if (fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
// move the file pointer to the end of the file
fseek(fp, 0L, SEEK_END);
// calculate the size by getting the current position of the file pointer
size = ftell(fp);
printf("Size of %s = %ld bytes\n", filename, size);
fclose(fp);
return 0;
}
In this code, we use fseek() to move the file pointer to the end of the file and then use ftell() to get the current position of the file pointer, which is the size of the file.
Method 4: Using the fstat() Function
Similar to the stat() function, the fstat() function can also be used to get the file size. However, it takes a file descriptor as an argument instead of a file name.
#include <stdio.h>
#include <sys/stat.h>
int main()
{
int fd;
struct stat st;
char filename[] = "example.txt";
fd = open(filename, O_RDONLY);
if (fstat(fd, &st) == 0)
{
printf("Size of %s = %ld bytes\n", filename, st.st_size);
}
else
{
printf("Error getting file size\n");
}
close(fd);
return 0;
}
In this code, we use the open() function to get a file descriptor and pass it to the fstat() function. The size of the file is then available in the st_size field of the struct.
Conclusion
In this guide, we have explored various methods for determining file size in C. Whether you prefer using built-in functions or system calls, you now have a comprehensive understanding of how to get the file size in your C programs. With these techniques, you can confidently handle file size calculations in your projects.