Why does string extraction from a stream set the eof bit?

前端 未结 2 1340
梦如初夏
梦如初夏 2020-12-31 13:29

Let\'s say we have a stream containing simply:

hello

Note that there\'s no extra \\n at the end like there often is in a text

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 14:05

    Intuitively speaking, the EOF bit is set because during the read operation to extract the string, the stream did indeed hit the end of the file. Specifically, it continuously read characters out of the input stream, stopping because it hit the end of the stream before encountering a whitespace character. Accordingly, the stream set the EOF bit to mark that the end of stream was reached. Note that this is not the same as reporting failure - the operation was completed successfully - but the point of the EOF bit is not to report failure. It's to mark that the end of the stream was encountered.

    I don't have a specific part of the spec to back this up, though I'll try to look for one when I get the chance.

提交回复
热议问题