473,387 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
7 11430
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
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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: gerry | last post by:
I have an async OnStore event written in c# and registered in exchange 2003. Everything seems to be working fine other than the SaveToFile(). If there is an error in the iMessage.DataSource.Open (...
4
by: Marco | last post by:
Hi to All, I'm developing an application in VB .NET. I have a question: what is the best place to save personal settings of my users? I mean, when a user uses my application, it can create a...
4
by: Lars Netzel | last post by:
I just saw this program called Handy Backup 4.5 where you can add backup jobs as such... and I wonder where you normally save data in an application. I come from an ASP world and have started to...
13
by: Wraith Daquell | last post by:
Hello all, I am searching for a free database to use with .NET. I haven't worked with databases much but can manipulate them with a small knowledge of SQL; I need to be able to store possibly...
3
by: noridotjabi | last post by:
Say I'm writting a program. In this program for some reason I need to store data somewere were I will be able to access it again. I don't want to store it in a file because then it could be...
10
by: Paul Cheetham | last post by:
Hi, I am developing an application that needs to store some machine-specific settings. The application is going to be published on the network in order to keep the clients on the latest version....
2
by: Alan Silver | last post by:
Hello, I am designing a form that allows people to request the formation of a limited company. When they fill in the form, they have to supply a certain amount of information relevant to their...
3
by: =?Utf-8?B?Sm9u?= | last post by:
Hello, I have tried to use the app.config and settings.cs files to store my data (which I want to be user changeable at runtime). I can write to (what I assume is an object in memory) and it does...
4
by: moizpalitanawala | last post by:
Hello Friends I am having a problem in finding the solution for this Question. Please help me. I am having a number say "n". In this case let us consider n=3. What i have to do is find first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.