因此,这是我的代码,我不断收到分段错误。 如何格式化此代码以从文件读取一组数字?如何从文本文件中读取多个编号
我的输入如下所示:82,46,71,56,44,12,100,62,67,64,65,62,39,68,68,90,78,57,76,45,82等
#include <stdio.h>
int main()
{
FILE *input = fopen("input.txt", "r");
int line[7];
int store = 0, read;
if(!input)
{
printf("Error: Filename \"input.txt\" not found!\n");
}
store = 0;
while(fscanf(input, "%d", &read) != EOF)
{
line[store] = read;
store++;
}
printf("%d %d %d %d %d %d %d\n", line[0], line[1], line[2], line[3], line[4], line[5], line[6]);
return(0);
}
什么是档案格式?在你的循环中检查'store <7',如果'input'是'NULL',不仅会打印错误消息,还会出现'exit(1)'。如果'fscanf'未能在没有输入错误的情况下读取'int',则其返回值将为0,因此请检查'fscanf(...)== 1'。 – 2013-03-20 23:50:11
在for循环中,似乎没有防范措施可以防止您[]行溢出。如果文件中有超过7个数字会怎么样? – dotcomslashnet 2013-03-20 23:50:37
好的,谢谢!我的输入看起来像这样: 82,46,71,56,44,12,100 62,67,64,65,62,39,68 68,90,78,57,76,45,82 等 这有助于我的情况呢? (另外,如果你知道,我该如何读下一行......) – 2013-03-20 23:52:51