眼眸繁星
基于NilObject的代码:#include #include off_t fsize(const char *filename) {
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
return -1; }变动:使文件名参数成为const char.修正struct stat定义,它缺少变量名。回报-1论错误而不是0,这对于空文件来说是不明确的。off_t是一个有符号的类型,所以这是可能的。如果你想fsize()若要打印错误消息,可以使用以下命令:#include #include #include #include #include off_t fsize(const char *filename) {
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
fprintf(stderr, "Cannot determine size of %s: %s\n",
filename, strerror(errno));
return -1;}在32位系统上,您应该使用以下选项进行编译-D_FILE_OFFSET_BITS=64,否则off_t将只保存高达2GB的值。参见“使用LFS”一节Linux中的大型文件支持关于细节。