如何在C中读取输入直到空行

如何在C中读取输入直到空行,c,C,我仍然是C语言的初学者。我正在尝试编写一个程序来读取输入(测试用例),直到到达一个空行 例如,输入如下: 2 1 2 2 5 3 6 2 3 1 7 第一个数字代表测试用例的数量。在输出中,每行上应该有数字的总和。 输出: 它们之间应该有一条空行,就像示例输出中的一样 #include <stdio.h> typedef struct { int a; int b; } A; int main() { int n, i = 0; A nums[

我仍然是C语言的初学者。我正在尝试编写一个程序来读取输入(测试用例),直到到达一个空行

例如,输入如下:

2
1 2
2 5
3 6

2 3
1 7
第一个数字代表测试用例的数量。在输出中,每行上应该有数字的总和。 输出:

它们之间应该有一条空行,就像示例输出中的一样

#include <stdio.h>

typedef struct {
    int a;
    int b;
} A;

int main() {
    int n, i = 0;
    A nums[100];
    scanf("%d\n", &n);
    while(n--){
        while(empty line not reached){
            scanf("%d %d", &nums[i].a, &nums[i].b);
            i++;
        }
        for(int j = 0; j < i; j++) printf("%d\n", nums[i].a + nums[i].b);
        printf("\n");
        i = 0;
            
    }
    return 0;
}
#包括
类型定义结构{
INTA;
int b;
}A;
int main(){
int n,i=0;
A nums[100];
scanf(“%d\n”,&n);
而(n--){
while(未到达空行){
scanf(“%d%d”、&nums[i].a、&nums[i].b);
i++;
}
对于(intj=0;j

那么,当(未到达空行)时,我应该在那里写些什么呢?
如何让程序检测到空行并转到下一个测试用例?

空行只有一个符号。它是“新行”符号。因此,您应该寻找两个新的线条符号。如果您在非二进制模式下使用文件,您应该查找\n\n符号。

您可以使用
fgets()测试空行。
eg使用
fgets(line,sizeof line,stdin);如果(*line=='\n')/*处理空行*/。我建议您将所有的
scanf()
替换为
fgets()
,并更改相关代码。非常感谢!!成功了!!:)这回答了你的问题吗@Andreas我已经解决了,谢谢:)
#include <stdio.h>

typedef struct {
    int a;
    int b;
} A;

int main() {
    int n, i = 0;
    A nums[100];
    scanf("%d\n", &n);
    while(n--){
        while(empty line not reached){
            scanf("%d %d", &nums[i].a, &nums[i].b);
            i++;
        }
        for(int j = 0; j < i; j++) printf("%d\n", nums[i].a + nums[i].b);
        printf("\n");
        i = 0;
            
    }
    return 0;
}