确定大于 4GB 的文件的大小

发布于 2024-07-04 23:34:30 字数 253 浏览 7 评论 0原文

当前代码执行此操作,并且 fgetpos 确实处理大于 4GB 的文件,但查找返回错误,因此任何想法如何查找 file > 的末尾都可以。 4GB?

fpos_t currentpos;

sok=fseek(fp,0,SEEK_END);
assert(sok==0,"Seek error!");

fgetpos(fp,&currentpos);
m_filesize=currentpos;

The code currently does this and the fgetpos does handle files larger than 4GB but the seek returns an error, so any idea how to seek to the end of a file > 4GB?

fpos_t currentpos;

sok=fseek(fp,0,SEEK_END);
assert(sok==0,"Seek error!");

fgetpos(fp,¤tpos);
m_filesize=currentpos;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ま柒月 2024-07-11 23:34:30

这段代码在 Linux 上适用于我:

int64_t bigFileSize(const char *path)
{
    struct stat64 S;

    if(-1 == stat64(path, &S))
    {
        printf("Error!\r\n");
        return -1;
    }

    return S.st_size;
}

This code works for me in Linux:

int64_t bigFileSize(const char *path)
{
    struct stat64 S;

    if(-1 == stat64(path, &S))
    {
        printf("Error!\r\n");
        return -1;
    }

    return S.st_size;
}
冬天旳寂寞 2024-07-11 23:34:30

如果您使用的是 Windows,则需要 GetFileSizeEx (MSDN)< /a>. 返回值是一个 64 位 int。

在 Linux 上 stat64 (manpage) 是正确的。 fstat 如果您正在使用 FILE*。

If you're in Windows, you want GetFileSizeEx (MSDN). The return value is a 64bit int.

On linux stat64 (manpage) is correct. fstat if you're working with a FILE*.

眉目亦如画i 2024-07-11 23:34:30

忽略所有出现“64”的答案。 在 Linux 上,您应该将 -D_FILE_OFFSET_BITS=64 添加到 CFLAGS 并使用 fseekoftello 函数来获取/返回 off_t< /code> 值而不是 long。 它们不是 C 的一部分,而是 POSIX 的一部分。 其他(非 Linux)POSIX 系统可能需要不同的选项来确保 off_t 是 64 位; 检查你的文档。

Ignore all the answers with "64" appearing in them. On Linux, you should add -D_FILE_OFFSET_BITS=64 to your CFLAGS and use the fseeko and ftello functions which take/return off_t values instead of long. These are not part of C but POSIX. Other (non-Linux) POSIX systems may need different options to ensure that off_t is 64-bit; check your documentation.

微暖i 2024-07-11 23:34:30

(盗自 glibc 手册)

int fgetpos64 (FILE *stream, fpos64_t *position)

该函数与 fgetpos 类似,但文件位置以 fpos64_t 类型的变量返回指向哪个位置。

如果源代码在 32 位机器上使用 _FILE_OFFSET_BITS == 64 进行编译,则此函数可以在名称 fgetpos 下使用,因此可以透明地替换旧接口。

(stolen from the glibc manual)

int fgetpos64 (FILE *stream, fpos64_t *position)

This function is similar to fgetpos but the file position is returned in a variable of type fpos64_t to which position points.

If the sources are compiled with _FILE_OFFSET_BITS == 64 on a 32 bits machine this function is available under the name fgetpos and so transparently replaces the old interface.

合约呢 2024-07-11 23:34:30

Stat 在确定文件大小方面总是比 fseek 更好,对于非文件的内容它会安全地失败。
64 位文件大小是一个特定于操作的东西,在 gcc 上,您可以将“64”放在命令末尾,或者您可以强制它默认进行所有标准调用 64。 有关详细信息,请参阅编译器手册。

Stat is always better than fseek to determine file size, it will fail safely on things that aren't a file.
64 bit filesizes is an operating specific thing, on gcc you can put "64" on the end of the commands, or you can force it to make all the standard calls 64 by default. See your compiler manual for details.

怀中猫帐中妖 2024-07-11 23:34:30

至少在 Linux 上,您可以使用 lseek64 而不是 fseek。

On linux, at least, you could use lseek64 instead of fseek.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文