Why couldn't the EOF bit be set before trying to read beyond the EOF and failing?!
Streams are very general and can be used to read any kind of data. Not everything has a known end. Take for instance std::cin. It is not always possible to now if the user will enter more data or not.
How can I deal with this, other than by using stream iterators (because I'd need to iterate short int as well, and that doesn't work).
You are using get and read so I assume you are reading binary data (not text). Reading a short int would look something like this.
1 2
short value;
file.read(reinterpret_cast<char*>(&value), sizeof(value));
Streams are very general and can be used to read any kind of data. Not everything has a known end. Take for instance std::cin. It is not always possible to now if the user will enter more data or not.
OK, I didn't think of that.
You are using get and read so I assume you are reading binary data (not text). Reading a short int would look something like this.
1 2
short value;
file.read(reinterpret_cast<char*>(&value), sizeof(value));
The problem is: how to differentiate between failed input and end of file? Because if I set failbit to throw an exception, while ( read() ) will always cause an exception to be thrown.