I was wondering if there was a way to reset the eof state in C++?
The rewind() function in C++ sets the file position indicator to the beginning of the given file stream.
Note that any open file is automatically closed when the ifstream object is destroyed.
No, this is done automatically by the ifstream destructor.
For a file, you can just seek to any position. For example, to rewind to the beginning:
std::ifstream infile("hello.txt"); while (infile.read(...)) { /*...*/ } // etc etc infile.clear(); // clear fail and eof bits infile.seekg(0, std::ios::beg); // back to the start!
If you already read past the end, you have to reset the error flags with clear()
as @Jerry Coffin suggests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With