The failbit is designed to allow the stream to report that some operation failed to complete successfully. This includes errors such as failing to open the file, trying to read data that doesn't exist, and trying to read data of the wrong type.
The particular case you're asking about is reprinted here:
char buffer[10];
stream.read(buffer, sizeof(buffer));
Your question is why failbit is set when the end-of-file is reached before all of the input is read. The reason is that this means that the read operation failed - you asked to read 10 characters, but there weren't sufficiently many characters in the file. Consequently, the operation did not complete successfully, and the stream signals failbit to let you know this, even though the available characters will be read.
If you want to do a read operation where you want to read up to some number of characters, you can use the readsome
member function:
char buffer[10];
streamsize numRead = stream.readsome(buffer, sizeof(buffer));
This function will read characters up to the end of the file, but unlike read
it doesn't set failbit if the end of the file is reached before the characters are read. In other words, it says "try to read this many characters, but it's not an error if you can't. Just let me know how much you read." This contrasts with read
, which says "I want precisely this many characters, and it's an error if you can't do it."
EDIT: An important detail I forgot to mention is that eofbit can be set without triggering failbit. For example, suppose that I have a text file that contains the text
137
without any newlines or trailing whitespace afterwards. If I write this code:
ifstream input("myfile.txt");
int value;
input >> value;
Then at this point input.eof()
will return true, because when reading the characters from the file the stream hit the end of the file trying to see if there were any other characters in the stream. However, input.fail()
will not return true, because the operation succeeded - we can indeed read an integer from the file.
Hope this helps!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…