Easiest way to store and save objects generated by a C++ program?

R

Rolf Hemmerling

Hello !

Beginner's question:

What ist the easiest way to store and save objects in a file generated
by a C++ program, by using the "standard C++ library" and/or "Standard
Template Library ( STL )" ?

So I would like to generate some objects ( of different classes ) with a
C++ program and would like to make it permanent / persistent, so that
when calling the C++ program next time, I can load the objects from that
file and use it again ( or use the data stored in that objects )?

I dont´want to store text data in text files, with non-trivial methods
to save and restore the data. So I don´t want a data delimiter format.

Especially, as the data is edited/modified, I can´t read the data from a
read-only .RC resource file, as I must rewrite it too. And I am not
shure if writing to a .RC resource file is so easy.

The concrete task of my C++ course at our university ist to build a
little "football manager", where I must load the name of the football
teams and the team pairings, and the results of each game.

One solution might be

"The stream buffer classes" of STL.

I don´t want to use proprietary functions of GUI frameworks ( MFC, QT,
wxWindows, Borland libraries....).

The other solution is to store the data in a SQL database and to access
the data by ODBC driver, of course. BUt for time, the C++ course does
not focus on databases, so I am looking for an "easier" solution.

Sincerely
Rolf
 
S

Simon Elliott

Rolf Hemmerling said:
What ist the easiest way to store and save objects in a file generated
by a C++ program, by using the "standard C++ library" and/or "Standard
Template Library ( STL )" ?

it's not available from the STL as far as I know, but these days I
usually use XML for this kind of task. Reasons:

1/ XML is resilient to changes in my data. Customers expect that a new
version of an application will read the data from a previous version.

2/ XML is a standard.

3/ XML is human readable and can be edited with a text editor. (This can
have its disadvanatges...)

The main disadvantage of XML is that the files can get very big and it
can take a while to read and parse them. And it can be a bit of a pain
to store large chunks of binary data.

For simple XML projects which don't need DTDs or XSLs I use tinyxml:
http://sourceforge.net/projects/tinyxml
 
R

Rolf Hemmerling

Thanks for the answer,

indeed with Java, I can create "serializable" objects which may easily
be stored and transfered by streams.

Simon said:
For simple XML projects which don't need DTDs or XSLs I use tinyxml:
http://sourceforge.net/projects/tinyxml

Its just that I don´t want "overhead" in learning,

and especially:

The string data ( name of the soccer teams) is no problem to store,
the data of the team pairings is "relational" ( just 2 pointers to the
table with the name of the teams,

and storing relational data must not be easy with XML ?

So is XML good for this SIMPLE relational database table
teams:
teamNumber
teamName

game:
teamNumber1
teamNumber2
goals_for_team1
goals_for_team2
number_of_the_game

?


Sincerely
Rolf
 
S

Simon Elliott

Rolf Hemmerling said:
The string data ( name of the soccer teams) is no problem to store,
the data of the team pairings is "relational" ( just 2 pointers to the
table with the name of the teams,

and storing relational data must not be easy with XML ?

On the contrary. XML is quite useful for this, and especially good for
messy, asymmetric, hard-to-normalise data. I seem to recall that there
are even a few RDBMS products which implement SQL queries onto an XML
dataset.
So is XML good for this SIMPLE relational database table
teams:
teamNumber
teamName

game:
teamNumber1
teamNumber2
goals_for_team1
goals_for_team2
number_of_the_game

<?xml version="1.0" encoding="utf-8" standalone="No" ?>
<!--Soccer Project -->
<SOCCER>
<TEAMS>
<TEAM INDEX="1" NAME="Glasgow Rangers">
<TEAM INDEX="2" NAME="Glasgow Celtic">
</TEAMS>
<GAMES>
<GAME INDEX="27" LOCATION="Ibrox">
<TEAMS TEAM1="1" TEAM2="2">
<RESULTS GOALS1="3" GOALS2="0">
</GAME>
<GAME INDEX="34" LOCATION="Celtic Park">
<TEAMS TEAM1="1" TEAM2="2">
<RESULTS GOALS1="2" GOALS2="1">
</GAME>
</GAMES>
</SOCCER>
 
T

Thomas Matthews

Rolf said:
Hello !

Beginner's question:

What ist the easiest way to store and save objects in a file generated
by a C++ program, by using the "standard C++ library" and/or "Standard
Template Library ( STL )" ?

So I would like to generate some objects ( of different classes ) with a
C++ program and would like to make it permanent / persistent, so that
when calling the C++ program next time, I can load the objects from that
file and use it again ( or use the data stored in that objects )?

I dont´want to store text data in text files, with non-trivial methods
to save and restore the data. So I don´t want a data delimiter format.
[snip]

The easiest method is to have binary read and write methods for each
class. These methods would operate on each member individually.
For composite classes, they would call the binary read or write
member for each member.

Be aware that pointers do not serialize (or store permanently)
since there is no guarantee that 1) the program is loaded into
the same address space and 2) that your dynamic memory allocation
is the same for each execution.

For variable records, such as text, I recommend writing the
length out first, then the data.
Sincerely
Rolf

For a more complex system (and maybe more efficient), each
class should have a method that writes to a buffer, reads
from a buffer and reports the number of bytes it occupies
in a stream. A non-member function binary write function
can sum up the sizes, then allocate a buffer and have
all the objects write to the buffer. The buffer is then
written as one chunk and deleted.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
R

Rolf Hemmerling

Great !

Sincerely
Rolf
<?xml version="1.0" encoding="utf-8" standalone="No" ?>
<!--Soccer Project -->
<SOCCER>
<TEAMS>
<TEAM INDEX="1" NAME="Glasgow Rangers">
<TEAM INDEX="2" NAME="Glasgow Celtic">
</TEAMS>
<GAMES>
<GAME INDEX="27" LOCATION="Ibrox">
<TEAMS TEAM1="1" TEAM2="2">
<RESULTS GOALS1="3" GOALS2="0">
</GAME>
<GAME INDEX="34" LOCATION="Celtic Park">
<TEAMS TEAM1="1" TEAM2="2">
<RESULTS GOALS1="2" GOALS2="1">
</GAME>
</GAMES>
</SOCCER>
 
C

Corwin Joy

The boost group (boost.org) also has a portable C++ serilization
library under review that looks quite promising in terms of handling
versioning STL containers etc. You can get a copy of the library at
the member files section in the yahoo group
http://groups.yahoo.com/group/boost/files/.
Depending on what your needs are this might save a lot of time - plus
there is a good discussion of other libraries that are out there.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top