Get File Size in C

  1. Use the stat Function to Get File Size in C
  2. Use fstat Function to Get File Size in C

This article will explain several methods of how to get file size in C.

Use the stat Function to Get File Size in C

stat system call is the POSIX compliant function that can be used to retrieve various attributes of the given file. It takes two arguments - the first of which is the char pointer that should point to the file’s pathname, and the second argument is of type struct stat pointer which is described extensively in the function manual. Once the function call returns successfully, the user can extract the needed attributes of the file from the struct stat by accessing its members directly. The file size is stored in the st_size member of the structure. Note that, stat function does not require permissions on the file to retrieve the attributes, but the caller should have executed permission on every directory included in the pathname.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>

const char *filename = "input.txt";

int main(int argc, char *argv[]) {
    struct stat sb;

    if (stat(filename, &sb) == -1) {
        perror("stat");
        exit(EXIT_FAILURE);
    }

    printf("Inode number: %lu\n", sb.st_ino);
    printf("User ID of owner: %u\n", sb.st_uid);
    printf("Group ID of owner: %u\n", sb.st_gid);
    printf("Total file size: %lu bytes\n", sb.st_size);
    printf("Last status change:       %s", ctime(&sb.st_ctime));
    printf("Last file access:         %s", ctime(&sb.st_atime));
    printf("Last file modification:   %s", ctime(&sb.st_mtime));

    exit(EXIT_SUCCESS);
}

Output:

Inode number: 12638061
User ID of owner: 1000
Group ID of owner: 1000
Total file size: 126 bytes
Last status change:       Sat Feb  6 23:35:02 2021
Last file access:         Sat Feb  6 23:35:02 2021
Last file modification:   Sat Feb  6 23:35:02 2021

The st_size member represents the total size of the file in bytes. In case the given pathname is the symbolic link, this member denotes the length of the pathname pointer by the link. We implement an example that takes the filename from the const declared hardcoded string value in the above code sample, although the user may pass the filename as the program parameter as demonstrated in the following example. stat call returns -1 on failure and errno is set accordingly; otherwise zero is returned to denote the success.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>

int main(int argc, char *argv[]) {
    struct stat sb;
    char *filename = NULL;

    if (argc != 2) {
        printf("Usage: ./program filename\n");
        exit(EXIT_FAILURE);
    }

    filename = argv[1];

    if (stat(filename, &sb) == -1) {
        perror("stat");
        exit(EXIT_FAILURE);
    }

    printf("Inode number: %lu\n", sb.st_ino);
    printf("User ID of owner: %u\n", sb.st_uid);
    printf("Group ID of owner: %u\n", sb.st_gid);
    printf("Total file size: %lu bytes\n", sb.st_size);
    printf("Last status change:       %s", ctime(&sb.st_ctime));
    printf("Last file access:         %s", ctime(&sb.st_atime));
    printf("Last file modification:   %s", ctime(&sb.st_mtime));

    exit(EXIT_SUCCESS);
}

Use fstat Function to Get File Size in C

Alternatively, we can use the fstat function, which takes the file descriptor as the first argument to specify the target file. The file descriptor can be acquired by open system call, but be sure to close opened files when they are not needed using the close system call. Notice that all examples in this article print most of the struct stat members. You might need to extract the relevant information about the attributes using the additional functions, e.g. st_mode and st_dev.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>

const char *filename = "input.txt";

int main(int argc, char *argv[]) {
    struct stat sb;

    int fd = open(filename, O_RDONLY);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    if (fstat(fd, &sb) == -1) {
        perror("stat");
        exit(EXIT_FAILURE);
    }

    printf("Inode number: %lu\n", sb.st_ino);
    printf("User ID of owner: %u\n", sb.st_uid);
    printf("Group ID of owner: %u\n", sb.st_gid);
    printf("Total file size: %lu bytes\n", sb.st_size);
    printf("Last status change:       %s", ctime(&sb.st_ctime));
    printf("Last file access:         %s", ctime(&sb.st_atime));
    printf("Last file modification:   %s", ctime(&sb.st_mtime));

    close(fd);

    exit(EXIT_SUCCESS);
}
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C File

  • Read File Line by Line Using fscanf in C
  • Create a New Directory in C