我编写了一个简单的交互式程序,它需要用户输入。 读取特定输入后,它会采取特定操作,但首先会通过从流中读取任何剩余字符来检查用户是否输入了太多命令。
我的问题是:如果流中没有剩余字符,它就会卡住,直到用户按下回车键。有没有办法覆盖它?
这是检查输入流中剩余字符的函数:
int check_end_of_stream(void){
char c;
c=getchar();
for(; c!='\n' && c!=EOF; c=getchar())
if(c!=' ' || c!=','){
printf("You have written too many statements");
printf(" in your command line!\n");
return 0;
}
return 1;
}
提前致谢!
附言我在 Linux 中编程
最佳答案
另一种解决方案是使用 getche() 方法,它不会等待按下回车...
int check_end_of_stream(void){
char c;
for(c=getche(); c!='\n' && c!=EOF; c=getche())
if(c!=' ' || c!=','){
printf("You have written too many statements");
printf(" in your command line!\n");
return 0;
}
return 1;
}
关于c - 如果没有什么可读的,如何跳过 getchar()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16494333/