检测 C 中的 EOF
我正在使用以下 C 代码从用户处获取输入,直到 EOF 发生,但问题是此代码不起作用,它在获取第一个输入后终止。这段代码有什么问题?
float input;
printf("Input No: ");
scanf("%f", &input);
while(!EOF)
{
printf("Output: %f", input);
printf("Input No: ");
scanf("%f", &input);
}
I am using the following C code to take input from user until EOF occurs, but problem is this code is not working, it terminates after taking first input. What's wrong with this code?
float input;
printf("Input No: ");
scanf("%f", &input);
while(!EOF)
{
printf("Output: %f", input);
printf("Input No: ");
scanf("%f", &input);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
EOF
只是一个带有值(通常为-1)的宏。您必须针对EOF
进行测试,例如getchar()
调用的结果。测试流结束的一种方法是使用
feof
函数。请注意,“流结束”状态只会在读取失败之后设置。
在您的示例中,您可能应该检查 scanf 的返回值,如果这表明没有读取任何字段,则检查文件结尾。
EOF
is just a macro with a value (usually -1). You have to test something againstEOF
, such as the result of agetchar()
call.One way to test for the end of a stream is with the
feof
function.Note, that the 'end of stream' state will only be set after a failed read.
In your example you should probably check the return value of scanf and if this indicates that no fields were read, then check for end-of-file.
EOF
是 C 中的常量。您没有检查实际文件中的 EOF。你需要做这样的事情这里是feof的文档。您还可以检查 scanf 的返回值。它返回成功转换的项目数,如果到达文件末尾,则返回 EOF。
EOF
is a constant in C. You are not checking the actual file for EOF. You need to do something like thisHere is the documentation to feof. You can also check the return value of scanf. It returns the number of successfully converted items, or
EOF
if it reaches the end of the file.另一个问题是您仅使用
scanf("%f", &input);
进行阅读。如果用户输入无法解释为 C 浮点数的内容(例如“pi”),则scanf()
调用不会向input
分配任何内容,并且不会从那里进步。这意味着它将尝试继续读取“pi”,但会失败。鉴于其他发帖者正确推荐的对 while(!feof(stdin)) 的更改,如果您在其中输入“pi”,则会出现无限循环打印出输入的前一个值 并打印提示,但程序永远不会处理任何新输入。
scanf()
返回它对输入变量进行的赋值次数。如果它没有进行赋值,则意味着它没有找到浮点数,您应该使用诸如 char string[100];scanf("%99s", string); 之类的内容来阅读更多输入代码>.这将从输入流中删除下一个字符串(无论如何,最多 99 个字符 - 额外的char
用于字符串上的空终止符)。你知道,这让我想起了我讨厌
scanf()
的所有原因,以及为什么我使用fgets()
而不是,然后可能使用sscanf( )
。Another issue is that you're reading with
scanf("%f", &input);
only. If the user types something that can't be interpreted as a C floating-point number, like "pi", thescanf()
call will not assign anything toinput
, and won't progress from there. This means it would attempt to keep reading "pi", and failing.Given the change to
while(!feof(stdin))
which other posters are correctly recommending, if you typed "pi" in there would be an endless loop of printing out the former value ofinput
and printing the prompt, but the program would never process any new input.scanf()
returns the number of assignments to input variables it made. If it made no assignment, that means it didn't find a floating-point number, and you should read through more input with something likechar string[100];scanf("%99s", string);
. This will remove the next string from the input stream (up to 99 characters, anyway - the extrachar
is for the null terminator on the string).You know, this is reminding me of all the reasons I hate
scanf()
, and why I usefgets()
instead and then maybe parse it usingsscanf()
.作为起点,你可以尝试替换
为
as a starting point you could try replacing
with
您想要检查 scanf() 的结果以确保转换成功;如果没有,则以下三件事之一是正确的:
例子:
You want to check the result of scanf() to make sure there was a successful conversion; if there wasn't, then one of three things is true:
Example: