Jump to content
  • Advertisement
Sign in to follow this  
ontheheap

Help reading binary (C++)

This topic is 5035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wrote a small helper tool to covert a normal text file containing map information for my game into a binary file. The tool is only one file, and it works fine. Here is what gets filled from data in the text file:
struct _MAP
{
    int total_tiles;
    int tile_width, tile_height;
    int tiles_wide, tiles_high;
    std::vector<int> map;
    std::vector<std::string> tiles;
};




Here is how I'm writing out the new (binary) file: (MAP is a _MAP object which has already been filled with data)
ofstream LVL(outputfile.c_str(), ios::binary);
LVL.write((char*)(&MAP), sizeof(MAP));
LVL.close();




In this same file, I check to make sure the binary data was written correctly by reading it in and printing out the values:
ifstream test_write(outputfile.c_str(), ios::binary);
_MAP test_map;
test_write.read((char*)(&test_map),sizeof(test_map));
    
cout << "Total Tiles: " << test_map.total_tiles << endl;

for(int i = 0; i < test_map.total_tiles; i++)
     cout << "TILE: " << test_map.tiles << endl;

cout << "Tile Width: " << test_map.tile_width << endl;
cout << "Tile Height: " << test_map.tile_height << endl;
cout << "Tiles Wide: " << test_map.tiles_wide << endl;
cout << "Tiles High: " << test_map.tiles_high << endl;
cout << endl << endl;
test_write.close()




Everythings works fine, all of the data is written, read, and printed correctly. Now, in my game, I'm using the exact same method to read in the file created from that tool. ( map is a private _MAP object )
CMap(const char* name)
{
   std::ifstream fin(name, std::ios::binary);
   fin.read((char*)(&map),sizeof(_MAP));
}




The data that is /not/ contained in the vectors is being read fine, but everything else is garbage. Also, if I create a CMap object, I get a "This problem has encountered an error and must close," error after I exit. I'm guessing there's a dtor trying to delete memory that it doesn't own. So, why is my tool able to read the binary file just fine while my game isn't? I'm using the exact same code to read the file in the game as I am in the tool! I'm pretty sure I posted all of the relevant code, and if someone could take a look and perhaps give me a couple of suggestions that'd be awesome.

Share this post


Link to post
Share on other sites
Advertisement
A vector of strings will contain pointers. In the tool, those pointers are pointing to valid, allocated data that's owned by your current process. When you save the vector of strings, you're basically saving some pointers.

When you read those pointers back in later, those pointers are almost certainly no longer pointing to data that your process owns, and even if they were the data at the addresses those pointers point to is almost certainly different.

So, basically you need to make sure all your data is serialised properly - ie written out in a form containing all the information needed to read it back in.

Share this post


Link to post
Share on other sites
Alright, that makes a lot of sense. So now I need to find an alternative way of storing and writing that data. Any there any common/preferred ways of doing this?

Share this post


Link to post
Share on other sites
Quote:
Original post by bytecoder
I usually just write out each element seperately to the file.


But I'm not sure if that will work in this case because the sizes of the two vectors aren't known until after the text file is read in. Err, I guess I could do it this way, but I'm definitely going to look for a prettier solution.

Share this post


Link to post
Share on other sites
Quote:
Original post by ontheheap
Quote:
Original post by bytecoder
I usually just write out each element seperately to the file.


But I'm not sure if that will work in this case because the sizes of the two vectors aren't known until after the text file is read in. Err, I guess I could do it this way, but I'm definitely going to look for a prettier solution.


i really dont see any other way to do this..

also, you can write out the size of the vector, then each item inside that vector. in the case with strings, you must write out the length of the string, and then the byte array. you cannot just write an entire dynamic object (like a string or vector) to a file.


[Edited by - graveyard filla on January 5, 2005 11:04:59 PM]

Share this post


Link to post
Share on other sites
btw, heres 2 functions i wrote that might help


void Write_String(string &str,ofstream &file_out)
{
const char *tmp = str.c_str();
int l = str.length();

// first write the length
file_out.write((char*)(&l), sizeof(l));

// then write the string itself (char*)
file_out.write(tmp,str.length() * sizeof(char));
}

void Read_String(string &str,ifstream &file_in)
{
int str_len = 0;
char *buff;

//first read in how big the string is
file_in.read((char *)(&str_len),sizeof(str_len));

//allocate memory
buff = new char[str_len + 1];

//read string
file_in.read(buff,str_len);

//term string
buff[str_len]='\0';

//copy the buffer into the string
str = buff;

//delete the memory for the buffer
delete buff;
}





Share this post


Link to post
Share on other sites
Awsome, graveyard filla! If I could rate you up I would, but I gave you the highest ++ I could quite a while ago.


Not too long ago I was working on a map editor (winapi/c), and I had a neat little map format that I created. Basically, the header of the file contained the tile size data, map size, etc. Then, each tile was its own block and it could contain any number of layers. It was a lot better than what I'm trying to do now, I only wish I still had the code for it. I need to rewrite that, since it was really an awesome way to store maps.

It's definitely going to require a somewhat complex editor though, which is what I was trying to avoid. I gave Mappy a shot, but I really dislike the format it uses (including the tiles and map in one big file). Honestly, I enjoy writing tools more than the actual games, so maybe I can turn this project into something great. Probably not, but one can always dream...

Share this post


Link to post
Share on other sites
glad to help. what exactly are you making anyway? did your hd die or something with your old level editor? i give you props for enjoying map editors more then the game.. i absolutely despise my editor [grin].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!