Writing an STL map to disk.

D

DanielEKFA

Hey there :)

I was once told that the STL classes had member functions to write their
data to disk and to restore that data. Searching google (and why are there
no "stl" or "map" manpages?), it seems like someone was pulling my leg,
because I really can't find anything about it.

So, was I misinformed? Or perhaps this person was talking about some other
classes you could use to do such a thing?

TIA,
Daniel :)
 
P

Phlip

DanielEKFA said:
I was once told that the STL classes had member functions to write their
data to disk and to restore that data. Searching google (and why are there
no "stl" or "map" manpages?), it seems like someone was pulling my leg,
because I really can't find anything about it.

Prepare yourself for a shock, but not everything has a manpage. My Panasonic
TV doesn't, for example.

These hits look promising:

http://www.google.com/search?q=serialize+site:boost.org
 
A

Abecedarian

DanielEKFA said:
I was once told that the STL classes had member functions to write their
data to disk and to restore that data.

No, you probably mean RogueWave classes.
But you can write a for-loop with fwrite() for serialization and a
for-loop with fread() for deserialization. :)

::A::
 
D

DanielEKFA

Abecedarian said:
No, you probably mean RogueWave classes.

Could be, not sure... Checking it out.
But you can write a for-loop with fwrite() for serialization and a
for-loop with fread() for deserialization. :)

This serialization term in this context is new to me... Is this like
balancing a binary tree by writing it in pre-order to a file and then
restore it be reading back the data from the file?

Cheers,
Daniel :)
 
P

Phlip

This serialization term in this context is new to me...

"Serialize" is the generic term for turning structured data into a
restructured stream.

Then, the first big question is whether the stream should use text or binary
encoding. Prefer text until you find a hard reason to use binary.

So serializing to XML is safer and more humane than writing raw data with
fwrite(). That will narrow your options to read the file.

Next, your data needs structure. Text formats like XML provide that with ><
marks, linefeeds, etc. Binary formats often encode the length of each field
before the field.

One impressive binary format out there is simply a GZip of an XML file. The
only requirement was small file size, and if both your GZip and XML
libraries can stream, then you can read efficiently.
Is this like
balancing a binary tree by writing it in pre-order to a file and then
restore it be reading back the data from the file?

Like. But that changed the raw structure on the way. (And to balance, you
should either use a std::map with the balancing act built-in, or you should
write the tree straight into another tree, or you can skip the problem and
not prematurely optimize.)
 
A

Abecedarian

DanielEKFA said:
This serialization term in this context is new to me... Is this like
balancing a binary tree by writing it in pre-order to a file and then
restore it be reading back the data from the file?

std::map has iterators. You can just write in 'key=value' style
(similar to Windows INI files) to the file.

::A::
 
D

DanielEKFA

Phlip said:
"Serialize" is the generic term for turning structured data into a
restructured stream.

Then, the first big question is whether the stream should use text or
binary encoding. Prefer text until you find a hard reason to use binary.

So serializing to XML is safer and more humane than writing raw data with
fwrite(). That will narrow your options to read the file.

Next, your data needs structure. Text formats like XML provide that with
field before the field.

One impressive binary format out there is simply a GZip of an XML file.
The only requirement was small file size, and if both your GZip and XML
libraries can stream, then you can read efficiently.

Interesting. Thanks :) I have another project (a personal one) planned after
this one, and XML definitely seems like the way to go for storing data,
also for the less obvious reasons like readability. Even if it does add an
overhead sometimes (like DC++'s files.dcLst.bz2 vs. files.xml.bz2 if you
know what I'm talking about).
Like. But that changed the raw structure on the way. (And to balance, you
should either use a std::map with the balancing act built-in, or you
should write the tree straight into another tree, or you can skip the
problem and not prematurely optimize.)

Ah, yes... Not very tree savvy yet, have just recently received education on
binary (search) trees, black/red, DB optimized, etc. :)

For this project, we're just gonna go with a text file, and put the cool
stuff on the enhancements todo list (just below "deferred deletes" ;)

Thanks,
Daniel :)
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top