Is this the proper way to flush the C input stream?

前端 未结 4 1903
情深已故
情深已故 2020-12-07 04:03

Well I been doing a lot of searching on google and on here about how to flush the input stream properly. All I hear is mixed arguments about how fflush() is undefined for th

4条回答
  •  一个人的身影
    2020-12-07 04:34

    It depends on what you think of as "flushing an input stream".

    For output streams, the flush operation makes sure that all data that were written to the stream but were being kept buffered in memory have been flushed to the underlying filesystem file. That's a very well defined operation.

    For input streams, there is no well defined operation of what flushing the stream should do. I would say that it does not make any sense.

    Some implementations of the C standard library redefine the meaning of "flush" for input streams to mean "clear the rest of the current line which has not been read yet". But that's entirely arbitrary, and other implementations choose to do nothing instead.

    As of C11 this disparity has been corrected, and the standard now explicitly states that the fflush() function does not work with input streams, precisely because it makes no sense, and we do not want each runtime library vendor to go implementing it in whatever way they feel like.

    So, please by all means do go ahead and implement your clearInputBuf() function the way you did, but do not think of it as "flushing the input stream". There is no such thing.

提交回复
热议问题