fflush() function not working with stdin

▼魔方 西西 提交于 2019-12-02 06:01:05
Shafik Yaghmour

fflush is meant to work with output or update streams and using it with an input stream is undefined behavior, from cppreference C section for fflush:

The behavior is undefined if the given stream is of the input type or if the given stream is of the update type, but the last I/O operation was not an output operation.

undefined behavior is behavior that the standard does not specify and it means that the result of your program is unpredictable, which could mean behavior that seems normal or a crash or other undesirable effects.

cppreference is consistent with the language in the draft C99 standard section 7.19.5.2 The fflush function which says:

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

Although this is not really your question and it seems like you have figured out a work-around, the question C, flushing stdin covers some proper ways of flushing stdin.

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