boost binary_iarchive读取的错误数据

问题描述 投票:1回答:1

我正在尝试从二进制文件读取自定义数据

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <fstream>

struct Data 
{
    int xs, ys;
    double ullat, ullon, lrlat, lrlon;
    double minval, maxval;

   // and some vector of floats
};

namespace boost { 
namespace serialization {

template <class Archive>
void
serialize (Archive& ar, Data& d, const unsigned int version)
{
    ar & d.xs & d.ys;
    ar & d.ullat & d.ullon & d.lrlat & d.lrlon;
    ar & d.minval & d.maxval;
}

} // namespace serialization
} // namespace boost

int main (void)
{
    std::ifstream fin ("test.bin", std::ios::in | std::ios::binary);

    // the binary file was not created by boost serialization library. No header information
    boost::archive::binary_iarchive arc (fin, boost::archive::no_header); 

    Data d;
    arc >> d;
}

此后,d包含垃圾值(巨大的-ve或+ ve数字)。

如果我使用fstream::read来按照d的布局读取文件,则将获得正确的值。我无法弄清楚这里发生了什么。正如我在代码中评论的那样,二进制文件是由boost序列化库创建的。因此使用no_header标志。我使用十六进制查看器工具(在Linux上为ghex)检查了二进制文件,并且fstream::read返回正确的值。这里可能出什么问题?

我正在使用带有Boost 1.70和C ++ 17的gcc-8.3和VS2017

我正在尝试从二进制文件#include

#include #include 结构数据{...

c++ boost-serialization
1个回答
0
投票
正如我在代码中评论的,二进制文件不是由boost序列化库创建的。
© www.soinside.com 2019 - 2024. All rights reserved.