Serialization truncation?: xml_oarchive, ostringstream vs. ofstream

Hi, I was trying out boost serialization and ran into a snag. If I serialize the same object onto two xml_oarchives, one constructed with a ostringstream and one constructed with an ofstream, I get differing results - the file based xml is well formed, but the string based xml is missing its final closing tag. Here is the generating code snippet: // now serialize ostringstream os; boost::archive::xml_oarchive ar(os); ofstream ofs("/home/sternlbe/boo.xml"); boost::archive::xml_oarchive ar2(ofs); Portfolio& port = *p; ar << BOOST_SERIALIZATION_NVP(port); ar2 << BOOST_SERIALIZATION_NVP(port); string s = os.str(); LOG_DEBUG(logger) << "Xml string: \n" << s; For the file output, I get the (correct) result: <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE boost_serialization> <boost_serialization signature="serialization::archive" version="3"> <port class_id="0" tracking_level="1" version="0" object_id="_0"> <m_id>2124433</m_id> <m_name>This is a stub!</m_name> <m_isLoaded>0</m_isLoaded> </port> </boost_serialization> However, the string result is missing the final line (closing tag): <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE boost_serialization> <boost_serialization signature="serialization::archive" version="3"> <port class_id="0" tracking_level="1" version="0" object_id="_0"> <m_id>2124433</m_id> <m_name>This is a stub!</m_name> <m_isLoaded>0</m_isLoaded> </port> Any thoughts? Thanks in advance, Ben Visit our website at http://www.ubs.com This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message which arise as a result of e-mail transmission. If verification is required please request a hard-copy version. This message is provided for informational purposes and should not be construed as a solicitation or offer to buy or sell any securities or related financial instruments.

Hello, Benjamin.Sternlieb@ubs.com wrote:
I was trying out boost serialization and ran into a snag. If I serialize the same object onto two xml_oarchives, one constructed with a ostringstream and one constructed with an ofstream, I get differing results - the file based xml is well formed, but the string based xml is missing its final closing tag.
Here is the generating code snippet:
// now serialize ostringstream os; boost::archive::xml_oarchive ar(os); ofstream ofs("/home/sternlbe/boo.xml"); boost::archive::xml_oarchive ar2(ofs); Portfolio& port = *p; ar << BOOST_SERIALIZATION_NVP(port); ar2 << BOOST_SERIALIZATION_NVP(port); string s = os.str(); LOG_DEBUG(logger) << "Xml string: \n" << s;
You should always make sure the archive object has been closed and destroyed before accessing any results. Try this and it should work: ostringstream os; ofstream ofs("/home/sternlbe/boo.xml"); { boost::archive::xml_oarchive ar(os); boost::archive::xml_oarchive ar2(ofs); Portfolio& port = *p; ar << BOOST_SERIALIZATION_NVP(port); ar2 << BOOST_SERIALIZATION_NVP(port); } string s = os.str(); LOG_DEBUG(logger) << "Xml string: \n" << s; Best Regards, Martin
participants (2)
-
Benjamin.Sternlieb@ubs.com
-
Martin Ecker