我有下面的代码,
fflush(stdin);
print("Enter y/n");
scanf("%c",&a);在这里,它在给出input.it之前退出,看起来问题是因为它没有刷新输入缓冲区,这可能有一些垃圾characters.Is有任何替代刷新(标准输入).This代码片段在Solaris中工作,但它在Linux中不起作用。
发布于 2011-06-08 18:33:06
这在C FAQ中有很好的解释。另请参阅:explanation。建议的解决方案:
使用scanf的
fgets sscanfwhile((c = getchar()) != '\n‘&& c != EOF) /*放弃字符*/;
刷新标准输入在某些实现上有效的事实是。
尽管可移植程序不能依赖于此,但某些供应商确实实现了fflush,以便使fflush(
)丢弃未读字符。
发布于 2013-03-24 15:01:05
对于GNU上的C
您可以使用
__fpurge(stdin);
include stdio_ext.h header for accessing the function. Though the post is very old still I thought this might help some linux developers.发布于 2014-05-15 07:10:31
scanf(" %c",&c);或
scanf(" ");
//reading operation (gets(), fgets(stdin,...) etc)scanf()格式字符串中的空格将忽略任何空格,直到第一个非空格为止。
https://stackoverflow.com/questions/6277370
复制相似问题