简体   繁体   English

如何在 C++ 中到达.eof() 后 go 回到文件的开头?

[英]How to go back to the beginning of a file after reaching .eof() in C++?

I just tried this , but don't work, maybe because I'm reading character by character?我刚试过这个,但不工作,也许是因为我正在逐字阅读?

char character;

while (!file.eof()) {

    character = file.get();
    cout << character;

}

Did you try the accepted answer?您是否尝试过接受的答案? The call to clear is the key. clear呼叫是关键。

OK, it works.好的,它有效。 The trick is call诀窍是打电话

file.clear();
file.seekg(0, ios::beg);

just after the iteration.就在迭代之后。 Sorry:(对不起:(

If your input is a stream (data piped in from another program) then you will not be able to seek.如果您的输入是 stream(从另一个程序输入的数据),那么您将无法搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM