473,698 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,Germ any
/ / / http://www.hemmerling.com/

Jul 19 '05 #1
7 11512
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 "serializab le" 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_g ame

?
Sincerely
Rolf
--
/ / / Alone on the data highway...
/ / like on an allee in Hannover-Herrenhausen
/ / / The Hemmerling (R) WEB site - Rolf Hemmerling,Germ any
/ / / 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_team 1
goals_for_team 2
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="Celti c 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.l earn.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="Celti c 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,Germ any
/ / / 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,Germ any
/ / / 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
3405
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 ( ie. the message has moved ) the expected exception is raised and caught. However when the strm.SaveToFile() is executed the process just seems to die - no output file is generated , no exceptions raised , no system log entries , no more log()...
4
2491
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 personal profile (e.g. last files opened, custom settings, etc...). What is the best place to save them? At first, I thought to save them in the registry, in the CURRENT_USER section, but I'm not sure every user can have read/write access to it. ...
4
1550
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 get into Windows Forms and Desktop programming and for me it's always been a Database-Way to go when wanting to save data.. or in the Registry when it comes to usersettings. But in the application I was looking at I looked into the installation...
13
1349
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 large amounts of data, and the database should probably be only one or so files. Also, there will not be a server in the distributed application, just the database and the application itself. Can anybody help me? TIA-AA -WDaquell
3
3145
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 deleted by another program or user. So, is there anyway to save data onto a computer so that it will not apear as a file (actually idealy not apear as anything at all), but will not get overwritten by any other program or file. If this is OS...
10
4258
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. Because of this, I am unable to store these settings in the App.Config file, as this gets updated every time the application does, and there doesn't appear to be a way of preventing this. Most of my application settings are kept in the...
2
1347
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 choice (company name, address, various other options). I was thinking about doing this with a wizard control as that allows me to split the form into manageable chunks. One of the features is that they can optionally specify a number of
3
9744
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 seem to work...however, once the application is closed and reopened the changes are lost. How do I persist the information? Here is some sample code using both methods: //////Using Properties.Settings///////
4
1161
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 "n" multiples of "n". Store the answers somewhere. Find again the first "n" multiples of the answers. Store the another answers(N1) generated.
0
8667
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8880
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8853
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7708
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4357
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3034
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.