【发布时间】:2014-06-05 01:20:21
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
//#define true 0
typedef struct
{
char currency[8];
int exchangerate;
} randT;
void main()
{
int i, num;
char currency1[8], ch;
FILE *file = fopen("address", "r");
randT name[7];
while(fscanf(file, "%i", &name[i].exchangerate) != EOF)/*I think this is where my problem is*/
{
fscanf(file, "%s %i", &name[i].currency, &name[i].exchangerate);
//printf("%s %i\n", currency1, num);
//if (fscanf(file, "%i", ¤cy1) == EOF) break;
printf("%s %i\n", name[i].currency, name[i].exchangerate);
i++;
}
fclose(file);
}
它给出了分段错误(核心转储),我对 fscanf 函数等相当陌生。请帮忙! 我的文本文件如下所示: 杰夫 4 吉娜 5 杰弗里 6 金娜 7 杰夫 8 吉尼娜 9 杰夫兹 10
【问题讨论】:
-
您的 seg 错误是因为您从未初始化
i。 -
首先使用
%d而不是%i来读取和打印整数。您还需要学习如何在给定分段错误的情况下进行调试(提示,在 gdb 上阅读,然后尝试gdb a.out core)。它会告诉你哪条线路出错了。为什么name是randT类型的? -
这很有趣,你的问题看起来很像stackoverflow.com/questions/24049510/…
标签: c while-loop scanf