Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] Multiple archives in same stream
From: Kenny Riddile (kfriddile_at_[hidden])
Date: 2009-07-01 02:46:43


Chard wrote:
> Pardon me if this issue has been discussed earlier, but is there a
> particular technical issue why multiple archives cannot be serialized to
> (or, specifically, de-serialized from) the same stream?
>
> (I'm still on 1.38, so slap me if this works with 1.39+).
>
> Given the following example:
>
> #include <boost/archive/text_oarchive.hpp>
> #include <boost/archive/text_iarchive.hpp>
> #include <boost/serialization/serialization.hpp>
>
> void test_ser_app()
> {
> {
> {
> std::ofstream file("ser.txt");
> archive::text_oarchive arch(file);
> std::string s("Hello World");
> arch & s;
> size_t pos = file.tellp(); // [1]
> }
> {
> std::ofstream file("ser.txt", std::ios::app | std::ios::ate);
> archive::text_oarchive arch(file);
> std::string s("Goodbye World");
> arch & s;
> }
> } // [2]
> {
> std::string s1, s2;
> std::ifstream file("ser.txt");
> {
> archive::text_iarchive arch(file);
> arch & s1;
> }
> size_t pos = file.tellg(); // [3]
> try
> {
> archive::text_iarchive arch(file);
> arch & s2;
> }
> catch (archive::archive_exception &e)
> {
> std::cout << e.what() << std::endl;
> }
> }
> }
>
> The stream position reported at [1] is (correctly) 42, and examination of
> the text file (at [2]) shows the two archives serialized okay.
>
> However, the stream position at [3] appears to be at the end of the stream.
> Does the serialization read in a big chunk, regardless of whether it will be
> processing it all?
>
> If I hack in a set stream position at [3], then the library quite happily
> reads the second archive. Is it possible to have the library leave the
> stream pointer at the correct end position?

This doesn't directly answer your question, but I'm using some code that
  reads individual archives from a single file containing hundreds of
archives. Basically I just use the iostreams library to grab the
section of the stream I need by passing the stream through a
boost::iostreams::restriction prior to constructing the archive. In an
unrelated note, the iostreams library's compression filters are also
handy here...text and xml archives compress quite nicely :)


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net