Determining exact file sizes in Linux C programs is performed efficiently using the POSIX stat() system call, which inspects filesystem inode metadata directly without reading file bytes.
POSIX stat File Size Code Example
#include <stdio.h>
#include <sys/stat.h>
int main(void) {
struct stat st;
const char *filepath = "/etc/passwd";
if (stat(filepath, &st) == 0) {
printf("File: %s | Size: %ld bytes\n", filepath, (long)st.st_size);
} else {
perror("stat failed");
}
return 0;
}
Comments and corrections