Saving a Class Object Into a File

Here's the class:
1
2
3
4
5
6
7
class Frame
{
public:
   string Name;
   char FramePixels[20][20];
   int SequenceNum;
};


I want to be able to save this to a file, and be able to load it. Is there a simple way of doing this?
closed account (DSLq5Di1)
The method in that tutorial is an old C solution, simple to implement, but doesn't play nice with C++, and is machine/compiler dependent.

I'd overload the stream operators and output the data members to a plain text file. If you want something fancier, consider using the boost serialization library. http://www.boost.org/libs/serialization
I agree with sloppy9

I quite often use XML as my serialization format, but then I have boiler plate code available to do most of the work. For a one off, custom code would prob be ok. You only real bit of work would be code to read and write the array safely.

If you want to learn a reusable approach though, it could be worth looking at one of the established libraries. In addition to Boost.Serialization, you could consider a JSON or YAML library. I've only played with these (we use XML at work...), but they look more friendly than XML.

http://en.wikipedia.org/wiki/JSON
http://www.json.org/
http://www.w3schools.com/json/default.asp
http://stackoverflow.com/questions/245973/whats-the-best-c-json-parser

http://en.wikipedia.org/wiki/YAML
http://www.yaml.org/
http://stackoverflow.com/questions/365155/parse-yaml-files-in-c-c

And then there's XML

Andy
Last edited on
Topic archived. No new replies allowed.