Get A File’s Size In C

Ever wanted to get the size of a file from your C process? Guess what, you can! Amazing, I know. It is a basic function call to find the file size in c of your specified file.

I will provide the function, a sample program, and some executable examples.

Use The stat Function To Get File Size

The stat function provides a set of information to the user, its definition is as follows, as well there are 3 #includes required.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
 
int stat(const char *path, struct stat *buf);

The function takes two arguments. The file path, which can include files, sockets, unix domain sockets, directories because as we all know everything in Linux / Unix are files!

To get the information about our file, the stat function populates a C structure with a set of file information.

The stat structure definition from the stat manual page.

struct stat {
           dev_t     st_dev;     /* ID of device containing file */
           ino_t     st_ino;     /* inode number */
           mode_t    st_mode;    /* protection */
           nlink_t   st_nlink;   /* number of hard links */
           uid_t     st_uid;     /* user ID of owner */
           gid_t     st_gid;     /* group ID of owner */
           dev_t     st_rdev;    /* device ID (if special file) */
           off_t     st_size;    /* total size, in bytes */
           blksize_t st_blksize; /* blocksize for file system I/O */
           blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
           time_t    st_atime;   /* time of last access */
           time_t    st_mtime;   /* time of last modification */
           time_t    st_ctime;   /* time of last status change */
    };

The field within this structure we are interested in is off_t st_size that holds the files size in it. As you can see we can actually retrieve a lot of extra information from this structure regarding the file.

To grab this structure we need the path to the filename and a stat structure, for instance:

u_int32_t get_file_size(const char *file_name) 
{
        struct stat buf;
        if ( stat(file_name, &buf) != 0 ) return(0);
        return( buf.st_size );
}

This function takes the path as an argument and returns the file size in bytes. stat will return -1 on error, and zero on success.

Example Program To Get File Size In C

Putting all this information together, here is a quick program that uses the first command line argument as the file name or path to the file, and spits out the file size in C.

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
 
u_int32_t get_file_size(const char *file_name) 
{
        struct stat buf;
        if ( stat(file_name, &buf) != 0 ) return(0);
        return( buf.st_size );
}
 
int main(int argc, char **argv)
{
    fprintf(stdout, "%u\n", get_file_size(argv[1]));
 
    return 0;
}

Executing this program:

erik@debian:/home2/erik/experiments/fsize$ ./fsize .
4096
erik@debian:/home2/erik/experiments/fsize$ ls -l
total 16
-rwxr-xr-x 1 erik erik 8658 2011-03-07 14:12 fsize
-rw-r--r-- 1 erik erik  307 2011-03-07 14:12 fsize.c
erik@debian:/home2/erik/experiments/fsize$ ./fsize fsize.c 
307
erik@debian:/home2/erik/experiments/fsize$ ./fsize fsize
8658

Looking at the above output, I first passed in a “.” which means the current directory I am in. All Linux directories are of file size 4096 bytes. I then did an output of the current directory contents using ls which displays the binary file I am running, and the source code of the program in the .c file. As you can see the file sizes match! Yay it worked!

stat is a very useful function for getting other information about your file, it is not just for getting the file size.