C++ istream: Is gcount() always set after a read() even if it fails?

空扰寡人 提交于 2021-01-28 01:32:03

问题


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 a read() even if that read failed due to EOF 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!