无法在 c 中使用 fscanf 读取整数 [英] Failed to read integer using fscanf in c
本文介绍了无法在 c 中使用 fscanf 读取整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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 个整数组成.怎么了?
I get failed to read integer. The file is okay, it consists 4 integers. What is wrong?
推荐答案
您应该检查 fscanf
是否返回 4,即格式字符串中的输入数量:
You should be checking to see if fscanf
returns 4, the number of inputs in your format string:
if(fscanf(fin, "%d %d %d %d", &arr[0], &arr[1], &arr[2], &arr[3]) == 4)
来自手册页:
返回成功匹配和分配的输入项的数量,可以少于提供的数量,或者在早期匹配失败的情况下甚至为零.
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 中使用 fscanf 读取整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文