0
我想将输入文件中的数据存储到结构中,但我不确定我在做什么错误。 这是输入文件。如何从输入文件中读取结构信息c
4 2 1 1 3 1
1 1 2 1 3 1
1 1 5 3 1 1
每个的第一个数字应该存储为沙,第二个数字应该存储为宝。
这是到目前为止我的代码:
#include <stdio.h>
#include <string.h>
#define PIRATES
#define MAP_SIZE 3
//structures
struct pirate {
int dig;
int carry;
};
struct map {
int sand;
int treasure;
};
//functions
int hours_crew();
void scan_file();
//main function
int main() {
FILE * ifp = NULL;
struct map map[MAP_SIZE][MAP_SIZE];
int i, j;
char filename[30];
printf("You have arrived at Treasure Island!\n");
while (ifp == NULL) {
printf("What is the name of your map?\n");
scanf("%s", &filename);
ifp = fopen(filename, "r");
}
for(i=0; i<MAP_SIZE; i++) {
for (j=0; j<MAP_SIZE; j++) {
fscanf(ifp, "%d", &map[i][j].sand);
fscanf(ifp, "%d", &map[i][j].treasure);
}
}
for(i=0; i<MAP_SIZE; i++) {
for (j=0; j<MAP_SIZE*2; j++) {
printf("%d", map[i][j].sand);
printf("%d\n", map[i][j].treasure);
}
}
fclose(ifp);
return 0;
}
'为(J = 0;Ĵ
是的。谢谢! – Momo
改进的格式 –