Getting File Size in C on Linux
To determine file size in C, use stat() to populate a struct stat and read the st_size field. Here’s the correct approach: #include <sys/stat.h> #include <stdio.h> off_t file_length(const char *path) { struct stat st; if (stat(path, &st) == -1) { perror(“stat”); return -1; } return st.st_size; } Why off_t and not long Using off_t as…
