I've just encountered a curious situation in C++. I was doing something like:
istream in;
// ...
in.get(); // get a char (which turns out to be the last)
// curiously ios::eof bit doesn't get set just yet
c = in.peek(); // attempt to peek, return EOF and now set ios::eof bit
if(c == EOF) {
// realize shouldn't have gotten the last char for some reason
in.unget(): // attempt to unget, *fails* (because ios:eof bit was set)
}
Now I'm curious why peek sets the eof bit; I find this highly unintuitive. It's supposed to just peek not actually consume anything and shouldn't change the stream state. Also, why unget
subsequently doesn't work? Does the standard mandates all operations to be nop when good()
is false or something?