ifstream :: ifstream可以读取的最大文件大小是多少 [英] ifstream:: What is the maximum file size that a ifstream can read
问题描述
我试图使用ifstream读取一个3GB的数据文件,它给我错误的文件大小,而当我读取一个600MB的文件,它给了我正确的结果。除了错误的文件大小,我也无法使用ifstream读取整个文件。
I tried to read a 3GB data file using ifstream and it gives me wrong file size, whereas when I read a 600MB file, it gives me the correct result. In addition to wrong file size, I am also unable to read the entire file using 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文件的输出为
Whereas the output for 600 MB file was
Windows file size : 678761111
ifstream file size: 678761111
为了防万一,我还测试了5GB文件和300 MB文件,
Just in case, I also tested for 5GB file and 300 MB file,
5GB文件的输出为
Windows file size : 5430386900
ifstream file size: 1135419604
300MB文件的输出为
The output for 300MB file was
Windows file size : 318763632
ifstream file size: 318763632
$ b
It looks to me like it is reaching some limit.
我使用Visual Studio 2010在Windows机器上测试代码,该机器有大量的内存和磁盘空格。
I am testing the code using Visual Studio 2010 on a Windows Machine which has plenty of memory and disk space.
我试图读取一些大文件。
I am trying to read some large files. Which is a good stream reader to use if ifstream can't read large files?
推荐答案
我想你想说:
std::cout << " ifstream file size: " << fs.tellg().seekpos() << std::endl;
至少对于我放置的一个6GB文件,它可以正常工作。但我正在使用Visual Studio 2012进行编译。甚至您的原始代码在该环境下也正常工作。
At least that works correctly for a 6GB file I have laying around. But I'm compiling with Visual Studio 2012. And even your original code works fine too on that environment.
所以我怀疑这是std库中的一个错误VS 2010在VS 2012中得到修复。无论是在操作符重载为pos_type的错误或如果该类不是64位感知是未知的。我必须安装VS 2010来验证,但这可能是问题。
So I suspect that this is an bug in the std library on VS 2010 that got fixed in VS 2012. Whether it's a bug in the operator overloading for the pos_type or if that class isn't 64-bit aware is unknown. I'd have to install VS 2010 to validate, but that is likely the problem.
这篇关于ifstream :: ifstream可以读取的最大文件大小是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!