Storing Objects /Object Properties

So here is the situation:
I want to be able to store a bunch of objects (which will probably be be stored in a linked list but that is not finalised) In a file.
What would be the most efficient way of doing so?
Storing all the objects attributes and then reading them from the file when creating the objects on each run?
Or is there a way to store a whole object/datastructure as a binary file so It can be easily chucked into the program.

(im a little rushed so I'll post moer later)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class A
{
private:
 int prop1;
 int prop2;
};

typedef std::list < A > Container;

class WriteToFile
{
public:
   WriteToFile(ofstream& to) : m_file(to){}
   void operator()(const A& element) const
   {
         //here your code, which implements saving
         // but you need to use m_file as output stream
    }
private:
    ofstream& m_file;
};

Container myData;

.....
std::ofsrem to("serialization.dat");
std::for_each(myData.begin(), myData.end(), WriteToFile(to));


And of course you need another solution, if your class has pointers
Last edited on
Or is there a way to store a whole object/datastructure as a binary file so It can be easily chucked into the program.


In a word, no. At least not easily, or safely. Streaming is the preferred method (ie, what Denis posted above).
Topic archived. No new replies allowed.