Files contain different types and amounts of data to store. The stored data sets the size of the file. We can find and display the size of the file in C by using different methods. In this tutorial, we examine how to file the size of the file in C by expressing it in different units like byte, kilobyte, megabyte, etc.
Find File Size with stat() Function
Unix and Linux operating systems provide the stat
system call in order to get different attributes of the files and folders. This stat call is also implemented for the C programming language with the stat()
function in order to get file attributes. We can use the stat() function in order to get the file size in C. The stat() function returns the struct stat
which provides the file attributes. The file size is provided with the st_size
value for this struct.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
const char *file_name = "/home/ismail/db.txt";
int main(int argc, char *argv[]) {
struct stat sb;
if (stat(file_name, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
printf("File size: %lu bytes\n", sb.st_size);
exit(EXIT_SUCCESS);
}
Find File Size with fstat() Function
The alternative way to find file size is by using the fstat()
function. The fstat() function is used with the file descriptor that is already opened with the open() function. The fstat() function requires the file descriptor struct stat
to put the file size and other attributes.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
const char *filename = "/home/ismail/db.txt";
int main(int argc, char *argv[]) {
struct stat sb;
int fd = open(file_name, O_RDONLY);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
if (fstat(fd, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
printf("File size: %lu bytes\n", sb.st_size);
close(fd);
exit(EXIT_SUCCESS);
}
Display File Size As Kilobyte
By default, the struct stat returns the file size as bytes using the st_size
attribute. We can convert the file size into the kilobyte unit by dividing it into 1024 like below.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
const char *filename = "/home/ismail/db.txt";
int main(int argc, char *argv[]) {
struct stat sb;
int fd = open(file_name, O_RDONLY);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
if (fstat(fd, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
printf("File size: %lu bytes\n", sb.st_size);
printf("File size: %lu kilobytes\n", sb.st_size/1024);
close(fd);
exit(EXIT_SUCCESS);
}
Display File Size As Megabyte
By default, the struct stat returns the file size as bytes using the st_size
attribute. We can convert the file size into the megabyte unit by dividing it into 1048576 like below.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
const char *filename = "/home/ismail/db.txt";
int main(int argc, char *argv[]) {
struct stat sb;
int fd = open(file_name, O_RDONLY);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
if (fstat(fd, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
printf("File size: %lu bytes\n", sb.st_size);
printf("File size: %lu kilobytes\n", sb.st_size/(1024*1024));
close(fd);
exit(EXIT_SUCCESS);
}