File I/O problem
I''m working on a 2d tile-based scrolling engine and am having some problems with the maps. I have a map maker and a set of routines for loading/saving maps, but it stops loading tiles after the 108th.
All the save function does is write out all of the IDs of the tiles, that works fine. When I load stuff gets lost. Here''s the code:
int tagWorld::LoadMap(char *file)
{
FILE *mapFile;
mapFile = fopen(file, "r+");
fseek(mapFile, 0, SEEK_SET);
fread(&Maps[NumMaps].ID, sizeof(int), 1, mapFile);
fread(&Maps[NumMaps].MapColumns, sizeof(int), 1, mapFile);
fread(&Maps[NumMaps].MapRows, sizeof(int), 1, mapFile);
for(int COLUMN = 0; COLUMN < Maps[NumMaps].MapColumns; COLUMN++)
{
for(int ROW = 0; ROW < Maps[NumMaps].MapRows; ROW++)
{
fread(&Maps[NumMaps].Tiles[COLUMN][ROW].ID, sizeof(unsigned char), 1, mapFile);
Maps[NumMaps].Tiles[COLUMN][ROW].WorldX = COLUMN * 64;
Maps[NumMaps].Tiles[COLUMN][ROW].WorldY = ROW * 64;
}
}
fclose(mapFile);
Maps[NumMaps].TotalHeight = Maps[NumMaps].MapRows * 64;
Maps[NumMaps].TotalWidth = Maps[NumMaps].MapColumns * 64;
NumMaps++;
return (NumMaps-1);
}
First it loads the ID of the Map. No Prob there.
Then it loads the number of columns and rows in the map. This also works.
The problem is when it loads the IDs. It goes fine until it gets to the 109th read, it just reads in 0s from there to the end.
I see no reason why this should be happening. My birthday is tomarrow and I don''t want to be pissed on my birthday.
Thanks
"The Gates of Pearl have turned to gold, it seems you've lost your way."
I looked at your code and I have some questions. It might help me if you could explain your file format.
Also you should change your file open statement to:
if((mapFile=fopen(file, "r+b"))!=NULL) {
//Put code here
}
else {
//Print error messages
}
Also you should change your file open statement to:
if((mapFile=fopen(file, "r+b"))!=NULL) {
//Put code here
}
else {
//Print error messages
}
http://www.crosswinds.net/~druidgames/resist.jpg
SpazBoy_the_Mitey to the rescue!
I couldn''t for the life of me see what was going wrong here,
so I wrote a little dummy version of your code that I could
compile and run for testing...
Now lets have a look:
mapFile = fopen(file, "r+");
This is the line in your code where you open the file.
You are currently opening in TEXT mode.
I replaced this with:
mapFile = fopen(file,"rb+");
and the whole thing worked just dandy.
hope I helped - Spaz
----------
Disco Love For Everyone
I couldn''t for the life of me see what was going wrong here,
so I wrote a little dummy version of your code that I could
compile and run for testing...
Now lets have a look:
mapFile = fopen(file, "r+");
This is the line in your code where you open the file.
You are currently opening in TEXT mode.
I replaced this with:
mapFile = fopen(file,"rb+");
and the whole thing worked just dandy.
hope I helped - Spaz
----------
Disco Love For Everyone
----------"i think that all this talking and such is paining my head to astounding annoyance" - Erick"Quoting people in your tag is cool. Quoting yourself is even cooler" - SpazBoy_the_MiteyDisco Love For Everyone
Yah, text mode was probably the problem, but I just use the _l* functions in plce of the f* ones. I assume that the low level ones use binary, so I probably could have saved some trouble by adding a ''b'' to the fopen, but it works now.
"The Gates of Pearl have turned to gold, it seems you've lost your way."
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement