If an error occurs before the number of elements requested is read then both eofbit and failbit are set. This is done whether you reach the end of file or not, because all the read method knows is the number of elements read and is therefore guessing.
// The standard read pattern
amountRead = rdbuf()->sgetn(pElements, amountRequested);
GCount += amountRead;
if( amountRead != amountRequested )
State |= eofbit | failbit; // Read error. Hmm! We must be at EOF (Good Guess, but not always true)
// In: vc6 STL - istream standard header
_St |= eofbit | failbit;
// In: .Net 2003 STL
State |= ios_base::eofbit | ios_base::failbit; // short read
// In: GNU ISO C++ Library
__err |= (ios_base::eofbit | ios_base::failbit);
I do know it terminated because of a read error, but since we can not know how many characters the encoded source data represents, we can not avoid the error. Therefore we do not know if it is an attempt to read the EOF or an encoding error.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone
modified on Tuesday, July 14, 2009 2:36 PM