gpt4 book ai didi

c - 无法在 c 中使用 fscanf 读取整数

转载 作者:行者123 更新时间:2023-12-02 05:25:46 25 4
gpt4 key购买 nike

FILE *fin = fopen("figura.in", "r");
if(fscanf(fin, "%d %d %d %d", &int[0], &int[1], &int[2], &int[3]) == 1) {
printf("%d\t%d\t%d\t%d\n", int[0], int[1], int[2], int[3]);
} else {
printf("failed to read integer.\n");
}

我无法读取整数。该文件没问题,它包含 4 个整数。怎么了?

最佳答案

您应该检查 fscanf 是否返回 4,即格式字符串中的输入数:

if(fscanf(fin, "%d %d %d %d", &arr[0], &arr[1], &arr[2], &arr[3]) == 4) 

来自man page :

return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

关于c - 无法在 c 中使用 fscanf 读取整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13100014/

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