读书人

关于fflush函数,该怎么解决

发布时间: 2012-08-09 15:59:21 作者: rapoo

关于fflush函数
刚才问问题的时候有个朋友说fflush(stdin)不能保证清空stdin流,是这样吗?他说了以后我才意识到我之前遇到的一些问题也是如此,有时候程序不会出错,但有时就执行语句以后好像总有剩余的数据,输出的东西感觉总多了什么,请问要是用什么办法能将stdin里面的全部清空能?

[解决办法]
while (fgetc(stdin) != '\n');就行了.

这是POSIX规定:

C/C++ code
NAME       fflush - flush a streamSYNOPSIS       #include <stdio.h>       int fflush(FILE *stream);DESCRIPTION       If stream points to an output stream or an update stream in which the most recent operation was not input, fflush() shall cause any unwritten  data  for       that stream to be written to the file,  and the st_ctime and st_mtime fields of the underlying file shall be marked for update.
[解决办法]
http://blog.csdn.net/csu305/article/details/3321084

读书人网 >C语言

热点推荐