By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,265 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,265 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
--
/ / / Alone on the data highway...
/ / like on an allee in Hannover-Herrenhausen
/ / / The Hemmerling (R) WEB site - Rolf Hemmerling,Germany
/ / / http://www.hemmerling.com/

Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Rolf Hemmerling <he********@gmx.net> writes
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
--
Simon Elliott
http://www.ctsn.co.uk/


Jul 19 '05 #2

P: n/a
Thanks for the answer,

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

Simon Elliott wrote:
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
--
/ / / Alone on the data highway...
/ / like on an allee in Hannover-Herrenhausen
/ / / The Hemmerling (R) WEB site - Rolf Hemmerling,Germany
/ / / http://www.hemmerling.com/

Jul 19 '05 #3

P: n/a
Rolf Hemmerling <he********@gmx.net> writes
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>

--
Simon Elliott
http://www.ctsn.co.uk/


Jul 19 '05 #4

P: n/a
Rolf Hemmerling wrote:
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

Jul 19 '05 #5

P: n/a
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>


--
/ / / Alone on the data highway...
/ / like on an allee in Hannover-Herrenhausen
/ / / The Hemmerling (R) WEB site - Rolf Hemmerling,Germany
/ / / http://www.hemmerling.com/

Jul 19 '05 #6

P: n/a

Thank you very much !
Sincerely
Rolf

--
/ / / Alone on the data highway...
/ / like on an allee in Hannover-Herrenhausen
/ / / The Hemmerling (R) WEB site - Rolf Hemmerling,Germany
/ / / http://www.hemmerling.com/

Jul 19 '05 #7

P: n/a
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.
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.