C语言 C : 如何在一次使用 scanf 一个字符时检查键盘或文件的输入结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19045026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C : How to check end of input from keyboard or file while using scanf one char at a time
提问by Tessa
I have to write a program in C for class that reads in one character at a time and we have to use scanf. I got it to work with input from only the keyboard however, my teacher will be testing the code with file redirection as well.
我必须用 C 为类编写一个程序,一次读取一个字符,我们必须使用 scanf。我让它只处理来自键盘的输入,但是我的老师也会用文件重定向来测试代码。
int end; //return value of scanf
int length1; //length of string exp1
char temp, //temp char for input
char exp1[81]; //string
printf ("Please enter in a regular expression:\n");
end = scanf ("%c", &temp);
while (temp != '\n' || end == 1) { //check if end of input
length1 = check(temp, length1, exp1); //returns length of exp1
end = scanf ("%c", &temp); //scan next char for loop
}
I've been at this one tiny problem for hours trying to figure it out so any pointers are helpful. Thanks!
我一直在这个小问题上试图弄清楚几个小时,所以任何指示都是有帮助的。谢谢!
EDIT: I've tried using the kbhit() function in an if statement to check if they typed in the input or if the input is from a file. However, I don't have conio.h, is there any alternative while still using scanf?
编辑:我尝试在 if 语句中使用 kbhit() 函数来检查他们是否输入了输入或输入是否来自文件。但是,我没有 conio.h,在使用 scanf 的同时还有其他选择吗?
回答by
Your narrower-scope question has already been answered, but let me give you a piece of good advice. If you are not forced to use scanf()in class (i. e. you are writing real, production code), then
您的范围较窄的问题已经得到解答,但让我给您一个很好的建议。如果您不是被迫scanf()在课堂上使用(即您正在编写真实的生产代码),那么
Just get rid of scanf().It's evil. It does not do what you think it does.
只要摆脱scanf()。这是邪恶的。它不会做你认为它会做的事情。
If you want to get one character at a time, then use the function that gets one character at a time. And that is fgetc(stdin).
如果你想一次获取一个字符,那么使用一次获取一个字符的函数。那就是fgetc(stdin)。
int c;
while ((c = fgetc(stdin)) != EOF) {
/* process `c' */
}
回答by Sadique
You need to compare scanf output with EOF
您需要将 scanf 输出与 EOF
while(scanf("%c", &temp) != EOF)
{
}
You could convert this to something like:
您可以将其转换为以下内容:
do{
length1 = check(temp, length1, exp1); //returns length of exp1
end = scanf ("%c", &temp); //scan next char for loop
}while (temp != '\n' && end != EOF) ;
回答by Yu Hao
scanfreturns EOFif an input failure occurs before the first conversion (if any) has completed. So you may check on that. But you only read one character each time, that doesn't seem necessary since you already check if the return value if 1.
scanfEOF如果在第一次转换(如果有)完成之前发生输入失败,则返回。所以你可以检查一下。但是您每次只读取一个字符,这似乎没有必要,因为您已经检查了返回值 if 1。
I think the error is in the logic in your whilecondition, it should be:
我认为错误在于您的while条件的逻辑,它应该是:
while (temp != '\n' && end == 1)
// ^^

