1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <errno.h> #include <string.h> #include "file.h" #include "print.h" long file_size(FILE * const file) { fpos_t p; if ((fgetpos(file, &p) != 0) || (fseek(file, 0L, SEEK_END) != 0)) { return -1; } const long size = ftell(file); if (fsetpos(file, &p) != 0) { return -1; } return size; }