c++ - Boost序列化多个对象

标签 c++ serialization boost

我正在使用二进制序列化一个带有 boost 的类。我正在使用 ios::append 以继续将多个对象附加到该文件。我该如何检索所有存储的对象?

这是我的测试类,它尝试多次序列化并检索它们。我已经评论了我没有得到正确数据的失败点。

using namespace std;
 class Data {
 public:
double get_latitude() const {
    return _latitude;
}

double get_longitude() const {
    return _longitude;
}

void set_latitude(double _latitude) {
    this->_latitude = _latitude;
}

void set_longitude(double _longitude) {
    this->_longitude = _longitude;
}
private:
double _latitude;
double _longitude;
friend class boost::serialization::access;
// When the class Archive corresponds to an output archive, the
// & operator is defined similar to <<.  Likewise, when the class Archive
// is a type of input archive the & operator is defined similar to >>.
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
    ar & _latitude;
    ar & _longitude;
}
};

class DataTest: public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE( DataTest);
CPPUNIT_TEST(testMultipleSaveData);
CPPUNIT_TEST_SUITE_END();

public:

void testMultipleSaveData() {
    Data data;
    data.set_latitude(1.0);
    data.set_longitude(2.0);
    saveData(data);
    Data secondData;
    secondData.set_latitude(5.0);
    secondData.set_longitude(6.0);
    saveData(secondData);
    Data returnData;
    Data return2Data;
    {
        // create and open an archive for input
        std::ifstream ifs("data.dat", ios::binary);
        boost::archive::binary_iarchive ia(ifs);
        // read class state from archive
        ia >> returnData;
        ia >> return2Data;
        // archive and stream closed when destructors are called
    }
    CPPUNIT_ASSERT_EQUAL(data.get_latitude(), returnData.get_latitude());
    CPPUNIT_ASSERT_EQUAL(data.get_longitude(), returnData.get_longitude());
    //Failure on next line
    CPPUNIT_ASSERT_EQUAL(secondData.get_latitude(), return2Data.get_latitude());
    CPPUNIT_ASSERT_EQUAL(secondData.get_longitude(), return2Data.get_longitude());
}

void saveData(Data data) {
    std::ofstream ofs("data.dat", ios::binary | ios::app);
    boost::archive::binary_oarchive oa(ofs);
    oa << data;
}

 };
 CPPUNIT_TEST_SUITE_REGISTRATION( DataTest);

最佳答案

决定添加另一个答案以避免完全困惑。

您的问题是您序列化为 boost::archive::binary_oarchive 的单独实例。 Boost Archive 在文件的开头存储了一些内部信息(我不会称它为标题,因为 Boost Serialization 有单独的标题来存储版本号),你会收到此信息的两份拷贝,位于文件开头和数据序列化之间。

Boost Archive 并非为此类用途而设计。甚至像这样指定 boost::archive::no_header:

boost::archive::text_oarchive oa(ofs, boost::archive::no_header);

没有帮助,因为此选项配置了另一个 header ,其中包含版本号。您需要序列化到 Boost Archive 的同一实例。

关于c++ - Boost序列化多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4973473/

相关文章:

c++ - 遍历对象集合的最快方法

c++ - 文件大小方法 boost 库奇怪

logging - 什么是 Boost 日志 "debug output window"?

c++ - boost 业力 : how does this implicit call to transform_attribute work?(或没有?)

c++ - 是否可以在 extern "C" block 内运行 C++ 代码?

c++ - 具有邻接表的有向图的复制构造函数

c++ - 克隆/fork、信号量等进程同步算法编程时从哪里开始

java - 在 Java 中序列化静态属性

c++ - 通过ofstream输出到文件时 boost 序列化 “read access violation”

java - boolean 类型序列化和反序列化单元测试失败