问题
I am reading some data using an istream and read(). I would like to know if I can just test gcount() for the bytes or if I need to test some combination of good(), eof(), etc before calling gcount(). In other words, is gcount() always set after a read() even if that read failed due to EOF or some other internal problem?
Also if this is described in the standard or somewhere that you can cite. I'm using cplusplus.com as a reference and it says that gcount "Returns the number of characters extracted by the last unformatted input operation performed on the object." Can I interpret statements like "last operation" to mean last operation, whatever the outcome?
回答1:
Is
gcount()
always set after aread()
even if that read failed due toEOF
or some other internal problem?
Yes
gcounts()
's job is solely to the return the number of characters extracted from the last unformatted input operation. The Standard makes no distinction between the value of gcount()
when an extraction succeeds and when it fails. And obviously if the input operation could not extract characters then the value will be 0
.
So all you need to test if an extraction succeeded is by using it as the condition. Use only gcount()
in the condition only if you wish to determine if a certain amount of characters were extracted.
来源:https://stackoverflow.com/questions/22083335/c-istream-is-gcount-always-set-after-a-read-even-if-it-fails