【发布时间】:2018-05-16 15:57:36
【问题描述】:
我正在尝试将输入文件中的数据存储到结构中,但我不确定我做错了什么。 这是输入文件。
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;
}
【问题讨论】:
-
for (j=0; j<MAP_SIZE*2; j++) {在您的显示循环中...这是错误。那是你的问题吗? (你没有描述你的问题) -
是的。谢谢!
-
改进格式
标签: c arrays file input structure