[serialization] Multiple archives in same stream
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
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
#include #include 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 :)
participants (2)
-
Chard
-
Kenny Riddile