参考自linux下如何清空(刷新)stdin缓冲区 首先,fflush在C/C++/POSIX标准中只定义了处理输出流的行为,对于像stdin这种输入流,这是未定义行为undefined behavior,随便C/C++库怎么去实现都不算错.即使某个C/C++库对fflush(stdin)的处理是删除掉你硬盘上所有文件都没有错.所以你根本不要指望任何未定义行为能在不同平台下有相同的表现. 再来看看linux对fflush(stdin)的说法 代码: man 3 fflush 引用: For in
C和C++的标准里从来没有定义过 fflush(stdin).也许有人会说:"可是我用 fflush(stdin) 解决了这个问题,你怎么能说是错的呢?"的确,某些编译器(如VC6)支持用 fflush(stdin) 来清空输入缓冲,但是并非所有编译器都要支持这个功能(linux 下的 gcc 就不支持),因为标准中根本没有定义 fflush(stdin).MSDN 文档里也清楚地写着fflush on input stream is an extension to the C sta
int fflush(FILE *stream);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