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 :(
精彩评论