Boost logo

Boost Users :

Subject: [Boost-users] [serialization] Multiple archives in same stream
From: Chard (boost_at_[hidden])
Date: 2009-06-30 16:34:46


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?


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