I'm trying to make a function to get a map from a file for a rpg. The header values are coming in fine, but as soon as I try to read the body of the file I get garbage values.
I'm using this method:
Code:
map_t::map_t(char* base) {
char filename[255];
int i, j;
tiles = new tiles_t(base);
sprintf(filename,"maps/%s/%s.map",base,base);
FILE *fp = fopen(filename,"r");
if (fp == NULL) {
allegro_message("Unable to open file \"%s\"",filename);
exit(1);
}
fscanf(fp,"%d %d %d %d",&mapw,&maph,&screenw,&screenh);
tilew = SCREEN_W / screenw;
tileh = SCREEN_H / screenh;
map = new int*[mapw];
for (i = 0; i < mapw; ++i)
map[i] = new int[maph];
for (j = 0; j < maph; ++j)
for (i = 0; i < mapw; ++i) {
scanf("%d",&(map[i][j]));
if (map[i][j] < 0 || map[i][j] >= tiles->numtiles) {
allegro_message("Invalid tile value %d at (%d,%d) in file %s",
map[i][j],i,j,filename);
exit(1);
}
}
fclose(fp);
}
and this data:
Code:
10 10 5 5
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
It's a total mystery to me why fscanf would get some of the values and not others. Is there some C newline trick that I'm missing or what?