【发布时间】:2021-12-06 10:11:52
【问题描述】:
我正在使用 boost C++ 库将数据序列化为二进制和文本文件。但是在序列化数据时,它会添加我不想要的标题。有没有办法删除这些标题? 例如:
#include <boost/archive/text_oarchive.hpp>
#include <fstream>
class Frame{
public:
int x;
int y;
int z;
};
template <typename Archive>
void serialize(Archive& ar, Frame& f, const unsigned int version) {
ar& f.x;
ar& f.y;
ar& f.z;
}
uint32_t main () {
Frame f={1,2,3};
std::ofstream ofs;
ofs.open("TextFile.txt",std::ios::out);
boost::archive::text_oarchive write(ofs,boost::archive::no_header);
write << f;
ofs.close();
}
结果是:
00 1 2 3
但我想要的是:
1 2 3
即使在创建 text_oarchive 对象时使用了 no_header,它仍然会给出这个 '00' 我想摆脱它。那么,如何去除呢?
【问题讨论】:
-
为什么要摆脱它?
-
如果您只想将一些人类可读的文本写入文件,您可以为您的类型重载
operator<<。如果您希望以后能够恢复序列化对象,则需要更多 -
(Boost 存档)添加了我不想要的标题 那么您将不需要使用 Boost 存档。
-
@Eljay 除了 boost::archive 在 boost 库中还有其他方法吗?
-
比什么好?你想达到什么目的?为什么标题让您感到不安?
标签: c++ serialization visual-c++ boost boost-serialization