C Program to Check File Size – fseek/ftell and stat()

There are two standard ways to check file size in C: the portable fseek()/ftell() method that works with any FILE pointer, and the POSIX stat() method that reads file metadata without opening the file. This page covers both, with complete working programs, a comparison table, and when to use each.

Method 1 — fseek() and ftell()

The idea is simple:

  1. Open the file in binary read mode ("rb")
  2. Move the file pointer to the end with fseek(fp, 0, SEEK_END)
  3. ftell(fp) now returns the number of bytes from the start — the file size
#include <stdio.h>

long file_size(FILE *fp) {
    fseek(fp, 0, SEEK_END);   /* move to end of file */
    return ftell(fp);         /* current position = size in bytes */
}

int main(void) {
    char filename[256];

    printf("Enter the file name: ");
    scanf("%255s", filename);

    FILE *fp = fopen(filename, "rb");   /* binary mode for an accurate byte count */
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }

    printf("Size of %s is %ld bytes\n", filename, file_size(fp));

    fclose(fp);
    return 0;
}

Why these details matter

  • "rb" (binary mode), not "r" — on Windows, text mode translates line endings, which can throw off a byte count. Binary mode gives the true size on every platform.
  • SEEK_END, not the magic number 2 — older code writes fseek(fp, 0L, 2). The named constant SEEK_END is the same value but self-documenting and portable.
  • perror() for errors — prints the actual reason (e.g. “No such file or directory”) instead of a vague message.
  • scanf("%255s", …) — the width limit prevents a buffer overflow when the filename is longer than the array.

Method 2 — Using stat() (No Need to Open the File)

On Linux, macOS, and other POSIX systems, stat() reads a file’s metadata — including its size — without opening it or moving any pointer. It is the more robust approach for large files:

#include <stdio.h>
#include <sys/stat.h>

int main(void) {
    char filename[256];

    printf("Enter the file name: ");
    scanf("%255s", filename);

    struct stat st;
    if (stat(filename, &st) != 0) {
        perror("Error");
        return 1;
    }

    printf("Size of %s is %lld bytes\n", filename, (long long)st.st_size);
    return 0;
}

st.st_size holds the file size in bytes. Because stat() does not open the file or read its contents, it is fast and works correctly even for very large files.

Sample Output

Enter the file name: notes.txt
Size of notes.txt is 1024 bytes

If the file does not exist

Enter the file name: missing.txt
Error opening file: No such file or directory

fseek/ftell vs stat() — Which to Use?

fseek() + ftell() stat()
Portability Standard C — works everywhere POSIX (Linux, macOS); use _stat on Windows
Opens the file? Yes No
Very large files ftell returns long — may be limited st_size handles large files well
Best for Portable code, files you are already reading Just need the size, especially big files

Key Points

  • Always open in binary mode ("rb") when measuring size, to avoid line-ending translation on Windows
  • Use SEEK_END with fseek(), never the bare number 2
  • Check the return value of fopen()/stat() and report errors with perror()
  • ftell() returns -1 on error — production code should check for it

Related File Programs in C


As an Amazon Associate we earn from qualifying purchases.

Recommended Book

File handling in C is covered thoroughly in The C Programming Language by Kernighan and Ritchie (K&R) — the definitive reference. See more in our guide to the best C programming books.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>