开发者

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

开发者 https://www.devze.com 2023-03-10 14:49 出处:网络
I just tried this, but don\'t work, maybe because I\'m reading character b开发者_运维技巧y character?

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

精彩评论

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

关注公众号