是否推荐使用fseek()计算文件大小的方法? [英] Is it recommended method for computing the size of a file using fseek()?

查看:361
本文介绍了是否推荐使用fseek()计算文件大小的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中,我们可以使用fseek()函数找到文件的大小.喜欢,

In C, we can find the size of file using fseek() function. Like,

if (fseek(fp, 0L, SEEK_END) != 0)
{
    //  Handle repositioning error
}

所以,我有一个问题,是否推荐使用fseek()ftell()计算文件大小的方法?

So, I have a question, Is it recommended method for computing the size of a file using fseek() and ftell()?

推荐答案

如果您使用的是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.

这篇关于是否推荐使用fseek()计算文件大小的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆