Get the size of a file in C

This is an example C program illustrating getting file size using the Unix system call stat.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>

/* This routine returns the size of the file it is called with. */

static unsigned
get_file_size (const char * file_name)
{
    struct stat sb;
    if (stat (file_name, & sb) != 0) {
        fprintf (stderr, "'stat' failed for '%s': %s.\n",
                 file_name, strerror (errno));
        exit (EXIT_FAILURE);
    }
    return sb.st_size;
}

int main (int argc, char ** argv)
{
    int i;
    for (i = 0; i < argc; i++) {
        const char * file_name;
        unsigned size;

        file_name = argv[i];
        size = get_file_size (file_name);
        printf ("%20s has %d bytes.\n", file_name, size);
    }
    return 0;
}

(download)

The output of the example, when run like

./gfs gfs.c makefile

looks like this:

               ./gfs has 9182 bytes.
               gfs.c has 738 bytes.
            makefile has 146 bytes.


Copyright © Ben Bullock 2009-2018. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer