gpt4 book ai didi

c++ - ifstream::ifstream 可以读取的最大文件大小是多少

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:07:51 29 4
gpt4 key购买 nike

我尝试使用 ifstream 读取一个 3GB 的数据文件,但它给出了错误的文件大小,而当我读取一个 600MB 的文件时,它给出了正确的结果。除了错误的文件大小,我也无法使用 ifstream 读取整个文件。

这是我使用的代码

        std::wstring name;
name.assign(fileName.begin(), fileName.end());
__stat64 buf;
if (_wstat64(name.c_str(), &buf) != 0)
std::cout << -1; // error, could use errno to find out more

std::cout << " Windows file size : " << buf.st_size << std::endl;;


std::ifstream fs(fileName.c_str(), std::ifstream::in | std::ifstream::binary);
fs.seekg(0, std::ios_base::end);

std::cout << " ifstream file size: " << fs.tellg() << std::endl;

3GB 文件的输出是

 Windows file size : 3147046042
ifstream file size: -1147921254

而 600 MB 文件的输出是

 Windows file size : 678761111
ifstream file size: 678761111

为了以防万一,我还测试了 5GB 文件和 300MB 文件,

5GB 文件的输出是

Windows file size : 5430386900
ifstream file size: 1135419604

300MB 文件的输出是

Windows file size : 318763632
ifstream file size: 318763632

在我看来它已达到某个极限。

我正在具有大量内存和磁盘空间的 Windows 计算机上使用 Visual Studio 2010 测试代码。

我正在尝试读取一些大文件。如果 ifstream 无法读取大文件,使用哪个流读取器比较好?

最佳答案

我想你想说:

std::cout << " ifstream  file size: " << fs.tellg().seekpos() << std::endl;

至少对于我手头的 6GB 文件来说,这至少能正常工作。但我正在使用 Visual Studio 2012 进行编译。甚至您的原始代码在该环境下也能正常工作。

所以我怀疑这是 VS 2010 上的 std 库中的错误,已在 VS 2012 中得到修复。是否是 pos_type 的运算符重载中的错误或者该类是否支持 64 位尚不清楚。我必须安装 VS 2010 才能验证,但这很可能是问题所在。

关于c++ - ifstream::ifstream 可以读取的最大文件大小是多少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16324811/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com