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/