无法使用 ifstream 将 txt 文件的最后部分写入 cout
下面的代码将打印我正在使用的示例文本文件中的所有文本,除了最后一个小片段。我认为这与 eof 或我使用的字节大小没有按预期工作有关。
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]){
int length;
char* buffer;
//get file stream and open local file.
ifstream stream;
stream.open("SampleFile.txt", ios::binary);
stream.seekg(0, ios::end);
length = stream.tellg();
stream.seekg(0, ios::beg);
//read stream 1024 bytes at a time until all bytes of file are used
buffer = new char[1024];
bool eof = false;
while(!eof)
{
stream.read(buffer, 1024);
cout.write(buffer, 1024);
if(stream.eof())
eof = true;
//cout << i << endl;
//cout.write(buffer, 1024);
}
stream.close();
delete[] buffer;
return 0;
}
我缺少什么?
The code below will print all of the text from the sample text file I'm using except for the last little snippet of it. I think this has something to do with the eof or the byte size I'm using not working as I expect.
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]){
int length;
char* buffer;
//get file stream and open local file.
ifstream stream;
stream.open("SampleFile.txt", ios::binary);
stream.seekg(0, ios::end);
length = stream.tellg();
stream.seekg(0, ios::beg);
//read stream 1024 bytes at a time until all bytes of file are used
buffer = new char[1024];
bool eof = false;
while(!eof)
{
stream.read(buffer, 1024);
cout.write(buffer, 1024);
if(stream.eof())
eof = true;
//cout << i << endl;
//cout.write(buffer, 1024);
}
stream.close();
delete[] buffer;
return 0;
}
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如您所知,缓冲区大小错误。另一件事是读取少于 1024 个字符(如果您的文件没有恰好
n*1024
字节,则会在末尾发生)。利用 istream::gcount 的优势,它可以为您提供上次读取时提取的字符数:As you already know, you have a wrong size of buffer. The other thing is the read of less than 1024 characters (going to happen at the end if your file doesn't have exactly
n*1024
bytes). Take the advantage ofistream::gcount
which gives you number of characters extracted by last read:1)您没有正确计算最终缓冲区的大小。
2) 您没有正确识别所有可能的错误情况。
尝试:
PS. If you are really trying to copy the named file to standard out, there is a much easier way:
1) You are not correctly calcuulating the size of the final buffer.
2) You are not correctly recognizing all of the possible error conditions.
Try:
PS. If you are really trying to copy the named file to standard out, there is a much easier way:
看起来问题是您上次读取的内容并不恰好是缓冲区的大小。您需要将其作为特殊情况来处理。有关详细信息,请参阅 http://www.cplusplus.com/reference/iostream/istream/阅读/。
哦,看起来你的缓冲区有 8 个字节长,但你读取了 1024 个字节。那很糟糕。
Looks like the problem is that your last read doesn't happen to be exactly the size of your buffer. You need to handle that as a special case. For details, see http://www.cplusplus.com/reference/iostream/istream/read/.
Oh, and it looks like your buffer is 8 bytes long but you read 1024 bytes. That's bad.
既然您知道文件的大小,为什么不立即读取整个文件呢?
Since you know the size of the file, why not read the entire file at once?