Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Saving an object to file? serialization?

Found something here but it still lacks an example.
http://www.parashift.com/c++-faq-lite/serialization.html#faq-36.8
I've also read that seralization is not designed for in C++, is this true.
how do you save an object to file?
Thanks!
[266 byte] By [rssmps] at [2007-11-11 8:08:21]
# 1 Re: Saving an object to file? serialization?
The Boost library includes serialization capability.

http://www.boost.org/libs/serialization/doc/index.html
nspils at 2007-11-11 21:01:41 >
# 2 Re: Saving an object to file? serialization?
yeah, I saw this too.... the only thing is I'm forced to work with a broken ver of GCC 2.92 and a somewhat working ver of xlC compiler.
So I'll need to figure out how to install the boost on the AIX platform first in order to use it.
rssmps at 2007-11-11 21:02:41 >
# 3 Re: Saving an object to file? serialization?
"seralization is not designed for in C++" says who? That's complete nonsense. Serialization is the process of storing an object's state in a file or another repository so that it can be recovered later. I can't think of a programming language that doesn't allow this.
As others have noted, Boost has a fine serialization library but for small and simple objects, you can roll your own serialization code:
http://www.dev-archive.com/getHelpOn/LegacyLink/9487
Danny at 2007-11-11 21:03:40 >
# 4 Re: Saving an object to file? serialization?
well, that was my first impression...I may have been reading too much into it.
http://www.functionx.com/cpp/articles/serialization.htm

I'll take a look at the link you sent. Thanks
rssmps at 2007-11-11 21:04:41 >
# 5 Re: Saving an object to file? serialization?
Danny,
in your examples, you end up writing out the basic types.
So by extension, if I have an Type A object which includes a vector<Type B> than I loop through the vector and call the serialize() of Type B?

but then how do you represent start and end of a vector? when you flatten the obj out, how do you determine the size since each vector is different? perhaps write out the vector size and then write out the value of each item?
rssmps at 2007-11-11 21:05:40 >
# 6 Re: Saving an object to file? serialization?
Yes, you have to iterate through the elements of the vector and write each object element to a file. Since you're serializing a vector, not just a single object, you have to serialize the data members of the vector to the file: its size (i..e, number of elements for instance). Usually, you write all these bookkeeping data at the beginning of the file and then when you de-serialize the vector you first read how many elements are stored in the file, and read them in a loop.
Danny at 2007-11-11 21:06:44 >