Search code examples
c++cereal

cereal compiles and runs, but does not write to file


The following code does not write to file.

#include <cereal/types/vector.hpp>
#include <cereal/archives/xml.hpp>
{
    vector<int> v = { 1,2,3 };
    stringstream s;
    s << "cereal_test.xml";
    cereal::XMLOutputArchive  oarchive(s);
    oarchive(v); 
}

It compiles and runs apparently correctly.

If we cout << s << endl; out of scope we see in the console:

cereal_test.xml
<?xml version="1.0" encoding="utf-8"?>
<cereal>
        <value0 size="dynamic">
                <value0>1</value0>
                <value1>2</value1>
                <value2>3</value2>
        </value0>
</cereal>

What's missing?


Solution

  • A mistaken copy-paste from cereal's tuturial:

    It should be ofstream instead of stringstream

    {
        vector<int> v = { 1,2,3 };
    
        std::ofstream outFile("cereal_test.xml");
        {
            cereal::XMLOutputArchive  oarchive(outFile);
            oarchive(v);
        }
    }