This program finds the size of a file in bytes in C. The classic approach uses fseek() to move the file pointer to the end of the file, then ftell() to read its position — which equals the file’s size. We also show a second, more robust method using stat().
Method 1 — fseek() and ftell()
The idea is simple:
- Open the file in binary read mode (
"rb") - Move the file pointer to the end with
fseek(fp, 0, SEEK_END) 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 number2— older code writesfseek(fp, 0L, 2). The named constantSEEK_ENDis 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_ENDwithfseek(), never the bare number2 - Check the return value of
fopen()/stat()and report errors withperror() ftell()returns-1on error — production code should check for it
Related File Programs in C
- C Program for File Operations — fopen, fread, fwrite, fclose
- C Program to Delete a File using remove()
- C Program to Copy One File to Another
- C Program to Count Characters in a File
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.