C 标准输入结束时,FGET应停止

C 标准输入结束时,FGET应停止,c,stdin,fgets,C,Stdin,Fgets,我正在尝试使用fgets读取stdin,但是是在小的块中。我的问题是,我不确定在stdin结束时,fgets等于什么。从我的代码来看,它显然不是NULL或类似\n的另一端。正确的方法是什么 代码(从fgets开始一直循环,从不等于NULL): 改变 while(fgets(str1, sizeof str1, stdin) != NULL && fgets(str1, sizeof str1, stdin) != '\n'){ 到 或 fgets返回一个char*。您无法将其与

我正在尝试使用
fgets
读取
stdin
,但是是在小的块中。我的问题是,我不确定在stdin结束时,
fgets
等于什么。从我的代码来看,它显然不是
NULL
或类似
\n
的另一端。正确的方法是什么

代码(从fgets开始一直循环,从不等于NULL):

改变

while(fgets(str1, sizeof str1, stdin) != NULL && fgets(str1, sizeof str1, stdin) != '\n'){


fgets
返回一个
char*
。您无法将其与
'\n'
进行比较,后者是
字符。此函数在遇到
EOF
时返回
NULL
。您可以在
stdin
上按模拟
EOF

  • 在windows上按CTRL+Z组合键
  • linux上的CTRL+D

您可以这样做:

while (fgets(str1, sizeof str1, stdin) != NULL && str1[0] != '\n')
如果
fgets()
读取新行,它将其存储在字符串中,如果遇到
EOF
则返回
NULL
。 通过这种方式,您可以获得输入并首先测试
fgets()
是否遇到
EOF
,然后测试字符串中的第一个字符(
str1[0]
)是否为换行符。

请记住
fgets()
返回一个
char*
,因此您可以使用指针表示法
*str1
或数组表示法
str1[0]
来测试字符串的第一个字符。

/Rajat Sewal
#包括
#包括
#包括
typedef结构岛{
char*name;
char*打开;
char*关闭;
struct岛*next;
}岛

int main()
{
printf(“这是一个使用文件读取所需数据的链表示例!\n”);
printf(“动态创建结构!\n\n\n”);
//首先,我们需要一个名为island的函数来创建新的island结构
/*****************************************************/
island*创建(char*名称)
{
island*i=malloc(sizeof(island));
//在新结构上设置字段
i->name=strdup(name);//strdup复制字符串以便使用新副本
i->opens=“09:00”
i->closes=“17:00”
i->next=NULL;
返回i;
}
/*****************************************************/
无效显示(孤岛*开始)
{
island*i=start;
for(;i!=NULL;i=i->next){
printf(“名称:%s\n打开:%s-%s\n\n\n”,i->Name,i->opens,
i->closes);
}
}

/********************************************/`

`island *start = NULL;`
`island *i = NULL;`
`island *next = NULL;`
`int j;`
`char name[80];`
`printf("Enter ^Z to finish [in windows]\n");`
`printf("Enter the names of the island you want to create:");`
`for(; (fgets(name, 80, stdin)!= NULL); i = next) {`
    `printf("Enter the next name of the island you want to create:");`
    `next = create(name);`
   ` if (start == NULL)`
       ` start = next;`
   ` if (i != NULL)`
        `i->next = next;`
`}`
`display(start);`
`return 0;`

}

fgets
返回一个
char*
。您无法将其与
'\n'
进行比较,后者是
字符
fgets(str1,str1的大小,stdin)!='\n'
-->
*str1!='\n'
?抱歉,我复制错了:(.它不能正确地作为字符串,只是不能作为字符编译。
fgets()
的返回指针永远不会匹配字符串文本;您需要使用
strcmp()
来比较字符串。此外,您不想读取第二行(或区块)在第一个过程中。@user73236,在这种情况下,只需删除
while
循环。注意
fgets()
返回两个值中的一个:(1)遇到EOF(或流上的错误)时为NULL,以及(2)问题中的第一个参数str1的值。它不会返回任何其他值。如果我使用“while”(fgets)运行它(str1,str1的大小,stdin)!=NULL)当我输入一些东西后,它会等待我输入更多而不是退出。@user73236,如果你想在任何输入后退出程序,只需删除循环。如果你想在一次输入后退出,为什么会有一个循环是没有意义的。如果它都来自一次输入,如果你在一次输入中键入一些字符列表,我希望它继续读取如果命令行长度超过4(在本例中是str1的4),它将反复循环,每次读取接下来的几个字符。但是,当它到达行的末尾时,它只等待更多的输入。@user73236,那么,循环体中需要的是
if(strstrstr(str,“\n”))break;
if(strcpsn(str,“\n”)==3)break;
。请注意,为了使用这些函数,您需要包含
string.h
。请正确设置代码格式,并解释为什么这是一种解决方案。
while(fgets(str1, sizeof str1, stdin)){
while (fgets(str1, sizeof str1, stdin) != NULL && str1[0] != '\n')
`island *start = NULL;`
`island *i = NULL;`
`island *next = NULL;`
`int j;`
`char name[80];`
`printf("Enter ^Z to finish [in windows]\n");`
`printf("Enter the names of the island you want to create:");`
`for(; (fgets(name, 80, stdin)!= NULL); i = next) {`
    `printf("Enter the next name of the island you want to create:");`
    `next = create(name);`
   ` if (start == NULL)`
       ` start = next;`
   ` if (i != NULL)`
        `i->next = next;`
`}`
`display(start);`
`return 0;`