Tae
Tae

Reputation: 1695

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;

}

Upvotes: 1

Views: 3092

Answers (3)

Tae
Tae

Reputation: 1695

OK, it works. The trick is call

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

just after the iteration. Sorry :(

Upvotes: 0

Ben Jackson
Ben Jackson

Reputation: 93890

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

Upvotes: 0

D.Shawley
D.Shawley

Reputation: 59623

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

Upvotes: 2

Related Questions