I currently write a class for some IO operations. Some functions return whether the IO operation was successful. If I’m reading a file, I wonder if I should return std::ifstream::good() or !std::ifstream::fail() to indicate whether the IO operation was sucessful.
The difference comes from the eof bit and I’m not sure that I correctly understand it.
Suppose that I have a binary file with 4 bytes in it (1 integer).
Suppose that I read this integer.
My question is : will the eof flag be set after this operation or after the next IO operation (that will fail) ?
If it set directly after this operation, if my reading function return std::ifstream::good(), then the result will be false (but the integer was correctly read).
Can you explain me when the eof bit is set and what should I return at the end of my functions ?
The EOF flag is set after you attempt to read over the end of the stream.
Your function should return a reference to the original stream, and you should use it in a loop or in a conditional, like this: