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.