I want to read/write a binary file (I'm making a game, text-based for now, graphics later) I'm using this code for reading/writing:
Code:
void player::save()
{
cout << "Saving...";
ofstream save;
save.open("bin.dat", ios_base::binary);
if(!save.is_open())
{
cerr << "Error in file I/O";
exit(1);
}
save.write(reinterpret_cast<char *> (this), sizeof(this));
save.close();
}
void player::load()
{
player b;
ifstream load;
load.open("bin.dat", ios_base::binary);
if(!load.is_open())
{
cerr << "Error in file I/O";
exit(1);
}
load.read(reinterpret_cast<char *> (&b), sizeof(&b));
cout << "Loaded: " << b.name;
load.close();
}
I took this code from my book (2 chapters ahead from my current chapter), but it doesn't work, no errors/warnings/runtime problems, but the program doesn't save the info. When I execute load() in my program (after save(), the file exists) it couts only "Loaded: " and not the b.name, is this a reading or writing problem and how can I solve it?