[英]Why fgets is reading the input, even though there is EOF in the input string?
I read the man
page of fgets()
.我阅读了
fgets()
的man
页。 It states "Reading stops after an EOF or a newline".它声明“在 EOF 或换行符后停止读取”。 My code is as follows.
我的代码如下。
#include <stdio.h>
#define MAX 50
int main()
{
char buf[MAX];
fgets(buf, MAX, stdin);
printf("string is: %s\n", buf);
return 0;
}
I gave this input:Welcome to -1 kkWorld.我给了这个输入:欢迎来到-1 kkWorld。
Output is: string is: Welcome to -1 kkWorld Output is: string is: Welcome to -1 kkWorld
fgets
should stop reading when it sees -1 in the input.当
fgets
在输入中看到 -1 时,它应该停止读取。 Why is fgets reading even though there is -1 or EOF in the string?即使字符串中有 -1 或 EOF,为什么 fgets 仍在读取? Am I missing something here?
我在这里错过了什么吗? Please help.
请帮忙。
As stated by Adrian, inputing "-1" ends up being two characters;正如 Adrian 所说,输入“-1”最终是两个字符; '-' and '1'.
'-' 和 '1'。 To emulate an EOF character, you must enter the single EOF value constant.
要模拟 EOF 字符,您必须输入单个 EOF 值常量。
EOF can be input to the program with Ctrl-D (Unix/Linux), or CTRL-Z (Microsoft).可以使用Ctrl-D (Unix/Linux) 或CTRL-Z (Microsoft) 将 EOF 输入到程序中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.