无法使用 ifstream 将 txt 文件的最后部分写入 cout

发布于 2024-11-24 09:46:16 字数 773 浏览 6 评论 0原文

下面的代码将打印我正在使用的示例文本文件中的所有文本,除了最后一个小片段。我认为这与 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 技术交流群。

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

发布评论

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

评论(4

暮凉 2024-12-01 09:46:17

如您所知,缓冲区大小错误。另一件事是读取少于 1024 个字符(如果您的文件没有恰好 n*1024 字节,则会在末尾发生)。利用 istream::gcount 的优势,它可以为您提供上次读取时提取的字符数:

char buffer[1024];
while(stream)
{
    stream.read(buffer, 1024);
    cout.write(buffer, stream.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 of istream::gcount which gives you number of characters extracted by last read:

char buffer[1024];
while(stream)
{
    stream.read(buffer, 1024);
    cout.write(buffer, stream.gcount());
}
半岛未凉 2024-12-01 09:46:17

1)您没有正确计算最终缓冲区的大小。

2) 您没有正确识别所有可能的错误情况。

尝试:

while(stream) {
    stream.read(buffer, 1024);
    cout.write(buffer, stream.gcount());
}


PS. If you are really trying to copy the named file to standard out, there is a much easier way:

ifstream stream("SampleFile.txt", ios::binary);
std::cout << stream.rdbuf();

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:

while(stream) {
    stream.read(buffer, 1024);
    cout.write(buffer, stream.gcount());
}


PS. If you are really trying to copy the named file to standard out, there is a much easier way:

ifstream stream("SampleFile.txt", ios::binary);
std::cout << stream.rdbuf();
如此安好 2024-12-01 09:46:17

看起来问题是您上次读取的内容并不恰好是缓冲区的大小。您需要将其作为特殊情况来处理。有关详细信息,请参阅 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.

爱她像谁 2024-12-01 09:46:17

既然您知道文件的大小,为什么不立即读取整个文件呢?

buffer = new char[length];

stream.read( buffer, length );
cout.write( buffer, length );

delete[] buffer;

Since you know the size of the file, why not read the entire file at once?

buffer = new char[length];

stream.read( buffer, length );
cout.write( buffer, length );

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