在C清除输入缓冲区的另一种方法 [英] Alternate method for clearing input buffer in c
本文介绍了在C清除输入缓冲区的另一种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有其他的方法来清除输入缓冲区在C withut使用
Is there any other method to clear the input buffer in c withut using
fflush();
或
while(getchar()!='\n');
由于我已经到处阅读并不能找到任何其他方式做到这一点。
Because i have read it everywhere and cant find any other way to do it.
推荐答案
使用与fgets()
作为suggester的 @放松的最好的办法。
Using fgets()
as suggester @unwind best approach.
要刷新到行的结尾。
void FlushStdin(void) {
int ch;
while(((ch = getchar()) !='\n') && (ch != EOF));
}
如果标准输入
是所有准备刷新到尾行,称 FlushStdin()
或其他发布 scanf()的
,龟etc()
解决方案,将刷新到年底的下次的行。
If stdin
is all ready flushed to the end-of-line, calling FlushStdin()
or other posted scanf()
, fgetc()
solutions, will flush to the end of the next line.
请注意 scanf函数(%* [^ \\ n]的%* 1 [\\ N]);
做的不的工作,如果下一个字符
是的'\\ n'
。
Note scanf("%*[^\n]%*1[\n]");
does not work if the next char
is '\n'
.
这篇关于在C清除输入缓冲区的另一种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文