Saving class data to a binary file

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

Recommended Posts

Hello All, Alright, so I have a class that contains high score information and in this class, the constructor attempts to load score data from a file if it exists, and the destructor saves the score data. However, for some reason it doesn't work, and I wasn't sure it would to begin with. Can anybody give me a brief explanation of why it doesn't work, and maybe how I could get it to work? At this point it looks like I will just have to have external functions that save and load the class data. Here is the source:
bool cScores::SaveScores()
{
std::ofstream outputStream("High_Scores.dat", std::ios::binary);

if (outputStream)
outputStream.write((char*)this, sizeof(this));
else
return false;

return true;
}

{
std::ifstream inputStream("High_Scores.dat", std::ios::binary);

if (inputStream)
else
return false;

return true;
}


Let me know if you have questions, I am feeling kind of scatterbrained as I write this. Thanks, Svenjamin

Share on other sites
If your class contains anything more than POD (Plain Old Data), which it does as it has methods, simply saving it byte-by-byte to a file doesn't work very well. You can solve this problem in a few ways:

You could write each data member manually and individually to the file, and read them back in in the reverse order.

Or, you could use a library like boost::serialization to do the same thing, but in a more flexible way. It'll handle things like saving the data pointed to by a pointer, not the address, as well as only keeping one copy of each object in the file, no matter how many pointers there are to it.

Share on other sites
post your class declaration.. maybe there is sth wrong...

Share on other sites
You should use sizeof(*this) since sizeof(this) is merely a size of a pointer.

Share on other sites
Quote:
 Original post by rozz666You should use sizeof(*this) since sizeof(this) is merely a size of a pointer.
Yes and no [smile]

sizeof(this) is the size of a pointer, as you said. However, sizeof(*this) isn't much better, since it includes the size of the vtable and anything else the compiler wants to stuff in there.

It's ok to write a struct like this if it's a POD type - just plain data, no pointers, functions are ok so long as they're not virtual functions. However, as soon as your struct/class contains virtual functions, pointers, or complex member variables like STL types, you need to save each member seperately. That's usually called Serialization, Googling should give loads of relevant results.

Share on other sites
You mention a constructor and destructor. I don't see those anywhere about. Where are they?

(Note that a destructor cannot legally return anything, and cannot sanely throw an exception either. If you want to write something to a file whenever an object is cleaned up, you're going to have to bite the bullet and just assume it worked until the next time you examine the file.)

Share on other sites
Oh yeah, I forgot the constructor/destructor, but all they do is call LoadScores() and SaveScores() respectively.

I am using STL containers in this class, so it sounds like I am going to have to use serialization. I've never heard of that before, so thanks for pointing it out to me. I'll have a look at the boost::serialization thing too.

Thanks for the help everybody,
Svenjamin

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
14
5. 5

• 10
• 9
• 16
• 9
• 9
• Forum Statistics

• Total Topics
632593
• Total Posts
3007304

×