To test for the eofbit setting.
bool eof() const;
Use the eof() function to test for an eofbit setting in a stream being processed under some conditions. This end of file bit is not set by stream opening or closing, but only for operations that detect an end of file condition.
If eofbit is set in rdstate() true is returned.
// ewl-test is simply a one line text document ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz #include <iostream> #include <fstream> #include <cstdlib> const char* TheText = "ewl-test"; int main() { using namespace std; ifstream in(TheText); if(!in.is_open()) { cout << "Couldn't open file for input"; exit(1); } int i = 0; char c; cout.setf(ios::uppercase); //eofbit is not set under normal file opening while(!in.eof()) { c = in.get(); cout << c << " " << hex << int(c) << "\n"; // simulate an end of file state if(++i == 5) in.setstate(ios::eofbit); } return 0; }
Result:
A 41 B 42 C 43 D 44 E 45