开发者

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 b开发者_运维技巧y character?

char character;

while (!file.eof()) {

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

}


Did you try the accepted answer? The call to clear is the key.


If your input is a stream (data piped in from another program) then you will not be able to seek.


OK, it works. The trick is call

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

just after the iteration. Sorry :(

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜