Help loading map files
I need help loading a map file from a text file. I''ve got the format of it set up and everything, but I can''t seem to get the program to read in the data correctly. The function that i use is this:
//Set TileID
for(lyr=0;lyr<this->layers;lyr++)
{
for(int y=0;y<this->MapHeight;y++)
{
Datafile.getline(string,(this->MapWidth)*2,''\n'');
for(int x=0;x<(this->MapWidth)*2;x=x+2)
{
*tempstring= *(string+x);
this->Map[lyr][x/2][y].SetTileID(atoi)tempstring));
}
}
Datafile.getline(string,this->MapWidth*3);
}
Most of the variables are pretty self explanatory, but here is the data that I am tryin to load from the file:
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,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
Its just a bunch of zero''s so that I can test to see if it works, it kinda loads the files corrects until I tell it to load about 30 tiles, then it kinda of craps out and gives me this error:
The instruction at "0x0040382f" referenced memory at "0xb1049d34". The memory could not be "read". If anyone can tell me a better way to load the file, or what I''m doing wrong, it would be much appreciated.
Nazmir
Don''t use getline, just get one character at a time. If you want to speed the load up you''ll have to use some sort of compression algorith. The easiest speedup is to not write zeros to memory on load. It''s redundant. If you need to clear your map use ZeroMemory.
Ben
http://therabbithole.redback.inficad.com
Ben
http://therabbithole.redback.inficad.com
does this line even compile?
> this->Map[lyr][x/2][y].SetTileID(atoi)tempstring));
The parentheses around atoi look misplaced...
You should really read up on proper C++ stream IO, there was a featured article about just that topic here recently. What you should do is read and write each number individually using the >> and << stream operators. They will give you buffering (so no speed hit) and automatic conversion from text to whatever datatype (int) you are trying to read/write (so no need for the old C atoi family of functions).
Also C style strings (char arrays) and arrays in general are two of the most error prone structures ever invented. Prefer std::string and std::vector wherever possible (almost always). I bet your error is a stupid off-by one out of array bounds read somewhere...
regards,
BuschnicK
Life would be much easier if I had the source code.
blackfish.sourceforge.net
> this->Map[lyr][x/2][y].SetTileID(atoi)tempstring));
The parentheses around atoi look misplaced...
You should really read up on proper C++ stream IO, there was a featured article about just that topic here recently. What you should do is read and write each number individually using the >> and << stream operators. They will give you buffering (so no speed hit) and automatic conversion from text to whatever datatype (int) you are trying to read/write (so no need for the old C atoi family of functions).
Also C style strings (char arrays) and arrays in general are two of the most error prone structures ever invented. Prefer std::string and std::vector wherever possible (almost always). I bet your error is a stupid off-by one out of array bounds read somewhere...
regards,
BuschnicK
Life would be much easier if I had the source code.
blackfish.sourceforge.net
Life would be much easier if I had the source code.blackfish.sourceforge.net
Do you still need help? I normally use strtok() to parse the strings. You should really consider using stream but in case you want to just do the minimal changes to get the code working, here is the codes
13thAngel
|
13thAngel
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement