【发布时间】:2014-02-08 21:07:29
【问题描述】:
由于某些奇怪的原因,当我的文件以字节为单位变得太大时,会在停止读取正确数据的地方发生此错误。就好像缓冲区在某个点停止接收数据。
我尽可能简化了我的代码以确定问题所在,但我意识到这不是我的代码,而可能是某个地方的缓冲区?
如果我的文件是 14,034 字节并且我运行这个简单的代码:
ifstream inFile("text.txt"); //File is 14,034 bytes
char test;
while(inFile >> test) //This will stop looping at about the 768 - 769 loop
cout << test;
如果我用 for 循环强制它走得更远,结果是一样的:
ifstream inFile("text.txt"); //File is 14,034 bytes
char test;
for(int count = 0; count < 14034; ++count) //The loop will continue until the
test = inFile.get(); //end, but for whatever reason,
cout << test; //inFile stops at 768 - 769 again
现在:如果我使用相同的代码,但对于较小的文件(比如 900 字节),我似乎没有 那个问题。
完全相同的代码,只是文件大小不同
ifstream inFile("text.txt"); //File is 900 bytes
char test;
while(inFile >> test) //This loop will continue to the 900th loop without
cout << test; //problems.
完全相同的代码,只是文件大小不同
ifstream inFile("text.txt"); //File is 900 bytes
char test;
for(int count = 0; count < 900; ++count) //The loop will continue until the
test = inFile.get(); //end (900) without problems.
cout << test;
我在 Google 上搜索了我的问题,但一直没有找到解决方案。我发现的唯一一个有类似问题的线程是这个:
fopen - can't write more than 16K?
但即便如此,也没有人真正理解他们在说什么 - 所以这个问题在技术上是没有答案的。
【问题讨论】:
-
你检查是否
inFile.is_open()?任何大文件或只是一个大文件?文件系统损坏? -
其实,你在期待什么?
inFile >> test逐字节读取?operator>>在 char 之后读取 char 但会跳过空格。 -
是的,文件已打开。文件系统没有损坏。它不是任何大文件,就是那个文件。我试图逐字节获取。我测试了各种方法,看看它们之间是否存在差异,它们都返回了相同的结果。我发现了这个问题 - 当出于某种原因读取字符 0x1A 时,它会中断该过程。现在我只需要弄清楚为什么。
标签: c++ file size buffer ifstream