且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否推荐使用fseek()计算文件大小的方法?

更新时间:2023-11-25 11:01:10

如果您使用的是Linux或其他类似UNIX的系统,则需要的是stat函数:

If you're on Linux or some other UNIX like system, what you want is the stat function:

struct stat statbuf;
int rval;

rval = stat(path_to_file, &statbuf);
if (rval == -1) {
    perror("stat failed");
} else {
    printf("file size = %lld\n", (long long)statbuf.st_size;
}

在Windows的MSVC下,您可以使用 _stati64 :

On Windows under MSVC, you can use _stati64:

struct _stati64 statbuf;
int rval;

rval = _stati64(path_to_file, &statbuf);
if (rval == -1) {
    perror("_stati64 failed");
} else {
    printf("file size = %lld\n", (long long)statbuf.st_size;
}

与使用fseek不同,此方法不涉及打开文件或查找文件.它只是读取文件元数据.

Unlike using fseek, this method doesn't involve opening the file or seeking through it. It just reads the file metadata.