gpt4 book ai didi

c - 我试图终止使用 fgets() 函数的 while() 循环

转载 作者:行者123 更新时间:2023-11-30 21:16:13 24 4
gpt4 key购买 nike

char src[5][100];
size_t counter = 0 ; // counter for src as it is two dimensional
printf("Enter the string : ");
while( counter < 5 && (fgets(src[counter],100,stdin) != NULL) && src[counter][0] != '\0' ) {
counter++;
}

我正在尝试终止这个循环,一种方法是采用某种固定输入,例如退出或其他内容,我不想使用它。另一种方法是按 ctrl + D 告诉 EOF ,我也不想这样做。

最佳答案

fgets读取一行时,它包含终止行字符\n。如果用户只是按 enter,fgets 将返回字符串 "\n" 而不是空字符串。尝试更换条件

src[counter][0] != '\0' 

src[counter][0] != '\n' 

关于c - 我试图终止使用 fgets() 函数的 while() 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36336225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com