How to store an object and reuse it later in C++

D

dagao

I have an object and want to store it in a file. Then later I can read
in the object and obtain all the info in it.
How can I do this ? Thanks a lot.
DG
 
E

E. Robert Tisdale

dagao said:
I have an object and want to store it in a file.
Then, later, I can read in the object and obtain all the info in it.
How can I do this?
cat example.cc
#include <fstream>

class X {
private:
// representation
// . . .
public:
// functions
std::eek:stream& save(std::eek:stream&) const;
// constructors
X(std::istream&); // restore
};

int main(int argc, char* argv[]) {
std::ifstream is("saved0.dat", std::ios::in);
X x(is);
// . . .
checkpoint:
std::eek:fstream os("saved1.dat", std::ios::eek:ut);
x.save(os);
return 0;
}
 
O

osmium

dagao said:
I have an object and want to store it in a file. Then later I can read
in the object and obtain all the info in it.
How can I do this ? Thanks a lot.

This topic is called persistence. Add a save and a restore member function
to the class. They must be intelligent enough to know what and how to save
and subsequently reproduce a working (not necessarily identical) data set. I
would suggest experimenting with increasingly difficult cases. Say
something without pointers, then a linked list and then a tree. If you save
a tree wrong you can end up with terrible performance, which gets back to my
"not necessarily identical" comment.

In the linked list, for example, you don't save the pointers, you save the
"pointees". Probably in a text file. Remeber that structs can be padded;
number of bits an int is, in general unkown, big endian/little endian etc.,
can have negative impact in the future if this is more than a short term
thing such as a student exercise.
 
A

Andras Tantos

I have an object and want to store it in a file. Then later I can read
This topic is called persistence. Add a save and a restore member function
to the class. They must be intelligent enough to know what and how to save
and subsequently reproduce a working (not necessarily identical) data set. I
would suggest experimenting with increasingly difficult cases. Say
something without pointers, then a linked list and then a tree. If you save
a tree wrong you can end up with terrible performance, which gets back to my
"not necessarily identical" comment.

In the linked list, for example, you don't save the pointers, you save the
"pointees". Probably in a text file. Remeber that structs can be padded;
number of bits an int is, in general unkown, big endian/little endian etc.,
can have negative impact in the future if this is more than a short term
thing such as a student exercise.

There are library solutions for persistency too. For an example look at the
rOops lib:
http://www.rcs.hu/rIDE/rOops.htm (you can download it from
http://www.rcs.hu/download.htm)
Also you can take a look at the following two articles on the subject:
http://c.ittoolbox.com/documents/document.asp?i=2311
http://c.ittoolbox.com/documents/document.asp?i=2320
 
R

Ryan Mitchley

Hi all

Before I go chasing ghosts - has anyone encountered any difficulties using
the Move Constructor (Mojo) approach with the Intel compiler (7.1 or
higher).

I am finding that functions that take temporaries as parameters are never
being called, and function returns are causing references to invalid memory.

I was previously using gcc without any problems.

If no-one has any clue I guess I'll have to start digging.

Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top