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>
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