Possible Duplicate:
How to serialize in c++?
How to implement serialization in C++
I've been toying around with C++ more and more these days and have only had a couple experiences with ofstream at this point. Most of said experiences have been doing simple file output of variables and reading them back in with ifstream. What I haven't done is anything with objects.
Let's assume that I have an object that is being written to frequently (say a game, and the object is the character) Every time the character is hit, the hp is re-written, every time they defeat an enemy they are gaining experience.... my basic idea is to write a simple text-based dungeon crawling game. But how am I going to make some kind of an autosave file?? Do I just write out every attribute of my object to a file individually and then move onto bigger and better from there? If I had to do it right now that's how I'd go about doing it, but I can't help like feeling that there's an easier way than that....
Anyone care to help me output the contents of an entire object(and it's respective attributes) to a file?
Search the web and SO for "serialization". There are some bumps to watch out for: floating point, endianess and variable length fields (strings).
Good luck!
Take a look at this code :
This is a simple struct with overloaded >> and << operators. This allows you to output to a file like myOfstreamFile << obj; And read the other way around.
If you have say, a thousand of objects stored in a file you can simply put them in a container like this :
Of course you could provide other forms of input and output e.g. save to .xml format and parse it as a dom tree etc. This is just a sample.
EDIT : This will work for relatively simple objects. Look at serialization if you need something more complex
You could just write the object to a file by copying it's contents in memory.
But then you hit the tricky bits! You can't copy any items that are pointers to memory, because when you load them back in they don't own that memory. That means copying things like std::string which may internally contain their own memory allocation is also tricky.
Then even with standard types there are issues if you plan to read them on a different machine with a different number of bits, or a different byte order.
The process is called serialisation - there are a few standard techniques to make it easier.
Behind your simple question hides a complex theme. Please take a look to boost::serialization (here for instance). Any time spent to learning boost it's very rewarding.
Here's a hacky trick that will likely get coders here to rip out their hair and scream at me (this only works for static objects and not ones that use dynamic memory allocation):
Which results in:
Opening the file reveals the written data:
è Data input w, È d
The above trick allows for easy conversion into a byte-based format. Naturally this is hacky, but classes (or objects) should be expected to supply their own object-to-char array conversion process.
There's a nice library in Boost called Boost.Serialize. If you're looking for performance, it is probably not the right choice, but looking at the source code and usage may give you some ideas. Serialization/Deserialization can be tricky, especially when you have lots of nested components. JSON is a very nice format for serializing general objects.