Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Saving and reloading a container to/from disk

Reply
Thread Tools

Saving and reloading a container to/from disk

 
 
jacob navia
Guest
Posts: n/a
 
      03-31-2010
Hi

What would be the best way to save and reload later a container
to/from disk in C++?

Thanks
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-31-2010
On 03/31/10 07:07 PM, jacob navia wrote:
> Hi
>
> What would be the best way to save and reload later a container
> to/from disk in C++?


It depends, there isn't really a best way. Do you want portability?

http://www.boost.org/doc/libs/1_42_0...doc/index.html

Is a good place to start.

--
Ian Collins
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      03-31-2010
Ian Collins a écrit :
> On 03/31/10 07:07 PM, jacob navia wrote:
>> Hi
>>
>> What would be the best way to save and reload later a container
>> to/from disk in C++?

>
> It depends, there isn't really a best way. Do you want portability?
>
> http://www.boost.org/doc/libs/1_42_0...doc/index.html
>
> Is a good place to start.
>


Thanks.

I downloaded the boost libraries, and compiled them in my machine.
Unziped the source code is 269MB. Compilation took 14 minutes.

Machine Mac-pro OS X with 8 CPUs and 12GB RAM.

Then, I compiled the example of the serialization.
The class being saved/restored looks like this:

original schedule
6:24 bob
0x0x100200440 34?135'52.56" 134?22'78.3" 24th Street and 10th Avenue
0x0x1002004d0 35?137'23.456" 133?35'54.12" State street and Cathedral
Vista Lane
0x0x100200530 35?136'15.456" 133?32'15.3" White House

when restored, the restored stuff looks like this:
6:24
0x0x100200e30 34?135'52.56" 134?22'78.3" 24th Street and 10th Avenue
0x0x100200f40 35?137'23.456" 133?35'54.12" State street and Cathedral
Vista Lane
0x0x1002012c0 35?136'15.456" 133?32'15.3" White House


As you can see the name "bob" is missing.

The same bug appears with all other saved/restored class instances.
I am not fluent in C++ to figure out this, sorry.

But maybe this is a bug in the example, I can't determine what
is the reason.

jacob
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      03-31-2010
On 2010-03-31, jacob navia <(E-Mail Removed)> wrote:
> Hi
>
> What would be the best way to save and reload later a container
> to/from disk in C++?
>
> Thanks


I'm not sure how best to deal with binary data, but for
numbers and text, I would use JSON (escaping special
characters as appropriate, etc).

It's well-understood, lightweight and simple, and
portable across many languages.

With binary data you could base64-encode it or something,
but you'll be looking at significant bloat for large
structures. Or you could NUL-separate fields, replacing
actual NUL characters with \001s, and actual \001s with
\001\001s.

--
Andrew Poelstra
http://www.wpsoftware.net/andrew
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      03-31-2010
Andrew Poelstra a écrit :
> On 2010-03-31, jacob navia <(E-Mail Removed)> wrote:
>> Hi
>>
>> What would be the best way to save and reload later a container
>> to/from disk in C++?
>>
>> Thanks

>
> I'm not sure how best to deal with binary data, but for
> numbers and text, I would use JSON (escaping special
> characters as appropriate, etc).
>
> It's well-understood, lightweight and simple, and
> portable across many languages.
>
> With binary data you could base64-encode it or something,
> but you'll be looking at significant bloat for large
> structures. Or you could NUL-separate fields, replacing
> actual NUL characters with \001s, and actual \001s with
> \001\001s.
>


Well, but that is a significant development. I thought that the
STL would provide something to save/restore a container.
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      03-31-2010
On 2010-03-31, jacob navia <(E-Mail Removed)> wrote:
> Andrew Poelstra a écrit :
>> On 2010-03-31, jacob navia <(E-Mail Removed)> wrote:
>>> Hi
>>>
>>> What would be the best way to save and reload later a container
>>> to/from disk in C++?
>>>
>>> Thanks

>>
>> I'm not sure how best to deal with binary data, but for
>> numbers and text, I would use JSON (escaping special
>> characters as appropriate, etc).
>>
>> It's well-understood, lightweight and simple, and
>> portable across many languages.
>>
>> With binary data you could base64-encode it or something,
>> but you'll be looking at significant bloat for large
>> structures. Or you could NUL-separate fields, replacing
>> actual NUL characters with \001s, and actual \001s with
>> \001\001s.
>>

>
> Well, but that is a significant development. I thought that the
> STL would provide something to save/restore a container.


I haven't looked that deeply into it, but the only serialization
method I have heard of is to override the << >> operators so you
can work with istreams. From there you have to iterate over your
container, structuring the data as you see fit.

I can't of any way in general that STL containers could save
themselves without confusing the data with their own control
characters.

--
Andrew Poelstra
http://www.wpsoftware.net/andrew
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      03-31-2010
jacob navia wrote:

> Andrew Poelstra a écrit :
>> On 2010-03-31, jacob navia <(E-Mail Removed)> wrote:
>>> Hi
>>>
>>> What would be the best way to save and reload later a container
>>> to/from disk in C++?
>>>
>>> Thanks

>>
>> I'm not sure how best to deal with binary data, but for
>> numbers and text, I would use JSON (escaping special
>> characters as appropriate, etc).
>>
>> It's well-understood, lightweight and simple, and
>> portable across many languages.
>>
>> With binary data you could base64-encode it or something,
>> but you'll be looking at significant bloat for large
>> structures. Or you could NUL-separate fields, replacing
>> actual NUL characters with \001s, and actual \001s with
>> \001\001s.
>>

>
> Well, but that is a significant development. I thought that the
> STL would provide something to save/restore a container.


That would be a little tricky because everything is templated. Now, suppose
you want to save/restore a vector<T>. The most natural thing would be to use
operator<< and operator>> for serializing and deserializing vector elements.
That, however, would suppose that for elements of type T both operations are
truly inverse. This does not even hold for the standard types (e.g., double
or std::string).

Then, for set<T,C>, it is not clear what to do about the comparison
predicate. Also: would you save/restore allocator objects or would you
decide to ignore the issue?

For simple cases, there is std::copy() and the use of stream iterators.
Also, containers can be initialized from a pair of iterators. Everything
more complex needs a custom solution.


Best

Kai-Uwe Bux
 
Reply With Quote
 
Brian
Guest
Posts: n/a
 
      03-31-2010
On Mar 31, 9:59*am, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> jacob navia wrote:
> > Andrew Poelstra a écrit :
> >> On 2010-03-31, jacob navia <(E-Mail Removed)> wrote:
> >>> Hi

>
> >>> What would be the best way to save and reload later a container
> >>> to/from disk in C++?

>
> >>> Thanks

>
> >> I'm not sure how best to deal with binary data, but for
> >> numbers and text, I would use JSON (escaping special
> >> characters as appropriate, etc).

>
> >> It's well-understood, lightweight and simple, and
> >> portable across many languages.

>
> >> With binary data you could base64-encode it or something,
> >> but you'll be looking at significant bloat for large
> >> structures. Or you could NUL-separate fields, replacing
> >> actual NUL characters with \001s, and actual \001s with
> >> \001\001s.

>
> > Well, but that is a significant development. I thought that the
> > STL would provide something to save/restore a container.

>
> That would be a little tricky because everything is templated. Now, suppose
> you want to save/restore a vector<T>. The most natural thing would be to use
> operator<< and operator>> for serializing and deserializing vector elements.
> That, however, would suppose that for elements of type T both operations are
> truly inverse. This does not even hold for the standard types (e.g., double
> or std::string).
>
> Then, for set<T,C>, it is not clear what to do about the comparison
> predicate. Also: would you save/restore allocator objects or would you
> decide to ignore the issue?
>
>


I don't know any serialization library that does
anything with comparison predicates or the allocators.
It's certainly possible that users want to use
different comparison predicates in different contexts,
and that attempting to make them use the same one
would cause them problems.


Brian Wood
http://webEbenezer.net
(651) 251-9384
 
Reply With Quote
 
Brian
Guest
Posts: n/a
 
      03-31-2010
On Mar 31, 2:32*am, Ian Collins <(E-Mail Removed)> wrote:
> On 03/31/10 07:07 PM, jacob navia wrote:
>
> > Hi

>
> > What would be the best way to save and reload later a container
> > to/from disk in C++?

>
> It depends, there isn't really a best way. *Do you want portability?
>
> http://www.boost.org/doc/libs/1_42_0...doc/index.html


The Boost library has some weaknesses.
http://webEbenezer.net/comparison.html



Brian Wood
http://webEbenezer.net
(651) 251-9384
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      03-31-2010
Kai-Uwe Bux a écrit :
> That would be a little tricky because everything is templated. Now, suppose
> you want to save/restore a vector<T>. The most natural thing would be to use
> operator<< and operator>> for serializing and deserializing vector elements.
> That, however, would suppose that for elements of type T both operations are
> truly inverse. This does not even hold for the standard types (e.g., double
> or std::string).
>


Excuse me but I do not understand that. If I write a double value into a
file, I will obtain the same value when I read it later if I store it in
binary form.

fwrite(&Double,1,sizeof(double),stream);
fread(&Double,1,sizeof(double),stream);

will leave the value of Double unchanged. Obviously if you use the same
CPU type for bothoperations.

I just do not see how that could be wrong. Maybe you care to explain?

Thanks

> Then, for set<T,C>, it is not clear what to do about the comparison
> predicate.


The container could be read in an incomplete way so that most values are
retrieved but function pointers aren't.

> Also: would you save/restore allocator objects or would you
> decide to ignore the issue?
>


See above.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving performance when reloading xml file Bertram Hurtig Java 1 09-07-2007 08:46 PM
std::transform container => std::abs(container) Steven T. Hatton C++ 4 12-05-2004 07:10 AM
saving and reloading variables for interactive work Darren Dale Python 4 07-28-2004 08:19 PM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments