filesize

How do you determine the size of a file in C?

天大地大妈咪最大 提交于 2019-11-25 22:34:40
问题 How can I figure out the size of a file, in bytes? #include <stdio.h> unsigned int fsize(char* file){ //what goes here? } 回答1: Based on NilObject's code: #include <sys/stat.h> #include <sys/types.h> off_t fsize(const char *filename) { struct stat st; if (stat(filename, &st) == 0) return st.st_size; return -1; } Changes: Made the filename argument a const char . Corrected the struct stat definition, which was missing the variable name. Returns -1 on error instead of 0 , which would be