Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] EOF marker?
From: Lars Viklund (zao_at_[hidden])
Date: 2013-04-15 12:00:41


On Mon, Apr 15, 2013 at 02:59:01PM +0000, Ramey, Larry wrote:
> Is there a way to serialize an unknown number of items and then read them back? In essence, I'd like to do this:
>
> Ignore the variables being serialized, these are pointers to base classes, they go in and out just fine.
>
> try
> {
> ofstream ofs(fileName.c_str(),ios::binary);
> portable_binary_oarchive oa(ofs);
> //boost::archive::text_oarchive oa(ofs);
>
> oa<<qdc
> <<qdf
> <<qdenv
> <<qdmsg
> <<qsd
> <<qdfc
> <<qdce;

*snip*

> while( true )
> {
> ia>>qdc2;
> cout<<qdc2->GetElementDebugType()<<endl;
> delete qdc2;
> qdc2=NULL;
> }
>
>
>
> }
> catch(boost::archive::archive_exception& ae)
> {
> cout<<ae.what();
> }
> catch( std::exception& e)
> {
> cout<<e.what();
> }

To my knowledge, S11n expects the code that reads the data to be aware
of the exact order, count and types of things in the serialized stream.

This means that if you want a sequence of variable length, you need to
serialize and deserialize data from which you can derive the extents of
the sequence.

You might be able to piggyback on existing logic if you put all your
objects in a standard library container like a list or vector and
serialize _that_.

Otherwise, count the objects and write a sized integer denoting the
count, or write a sentinel object that you can use to find the end of
the sequence.

Depending on your actual use case and types involved, one may be easier
than the other.

-- 
Lars Viklund | zao_at_[hidden]

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