繁体   English   中英

为什么 fgets 正在读取输入,即使输入字符串中有 EOF?

[英]Why fgets is reading the input, even though there is EOF in the input string?

我阅读了fgets()man页。 它声明“在 EOF 或换行符后停止读取”。 我的代码如下。

#include <stdio.h> 
#define MAX 50 
int main() 
{ 
    char buf[MAX]; 
    fgets(buf, MAX, stdin); 
    printf("string is: %s\n", buf); 

    return 0; 
} 

我给了这个输入:欢迎来到-1 kkWorld。
Output is: string is: Welcome to -1 kkWorld
fgets在输入中看到 -1 时,它应该停止读取。 即使字符串中有 -1 或 EOF,为什么 fgets 仍在读取? 我在这里错过了什么吗? 请帮忙。

正如 Adrian 所说,输入“-1”最终是两个字符; '-' 和 '1'。 要模拟 EOF 字符,您必须输入单个 EOF 值常量。

可以使用Ctrl-D (Unix/Linux) 或CTRL-Z (Microsoft) 将 EOF 输入到程序中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM