【发布时间】:2013-11-07 21:59:59
【问题描述】:
我正在尝试从文本文件中获取一些输入,将其放入结构中并打印出来。示例文本文件如下所示:
2
Curtis
660-------
Obama
2024561111
(第一个号码上的数字被划掉(为了隐私),第二个是 Whitehouse.gov 的号码,我打过电话,他们帮不了我。)
示例输出:
204-456-1111 Obama
660--------- Curtis
(当我弄清楚其余部分时,格式和排序应该不是问题。)
我的问题由下面的问号标记(在第一个 FOR 循环中,如何从文本文件中获取特定行来创建结构?
#include <stdio.h>
#include <string.h>
struct telephone {
char name[80];
long long int number;
}
main() {
struct telephone a, b;
char text[80];
int amount, i;
FILE *fp;
fp = fopen("phone.txt", "r");
fscanf(fp, "%d", amount);
struct telephone list[amount];
for(i = 0; i < amount; i++) {
strcpy(list[i].name, ???);
list[i].number, ???);
}
fclose(fp);
for(i = 0; i < amount; i++) {
DisplayStruct(list[i]);
}
}
DisplayStruct(struct telephone input) {
printf("%lld %s\n", input.number, input.name);
}
【问题讨论】: