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<
*snip*
while( true ) { ia>>qdc2; cout<<qdc2->GetElementDebugType()<
} catch(boost::archive::archive_exception& ae) { cout<
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@acc.umu.se