Direct boost serialization to char array(直接提升序列化到字符数组)
问题描述
Boost 序列化文档断言序列化/反序列化项目的方法是使用二进制/文本存档,并在底层结构上使用流.如果我不想将序列化数据用作 std::string,这可以正常工作,但我的目的是将其直接转换为 char* 缓冲区.如何在不创建临时字符串的情况下实现此目的?
Boost serialization doc's assert that the way to serialize/deserialize items is using a binary/text archive with a stream on the underlying structure. This works fine if I wan't to use the serialized data as an std::string, but my intention is to convert it directly to a char* buffer. How can I achieve this without creating a temporary string?
解决了!对于那些想要一个例子的人:
Solved! For the ones that wanted a example:
char buffer[4096];
boost::iostreams::basic_array_sink<char> sr(buffer, buffer_size);
boost::iostreams::stream< boost::iostreams::basic_array_sink<char> > source(sr);
boost::archive::binary_oarchive oa(source);
oa << serializable_object;
推荐答案
IIUC,您想写入一个固定大小的预分配数组.
IIUC, you would like to write to a preallocated array of fixed size.
您可以使用 boost::iostreams::array_sink(用 stream 给它一个 std::ostream 接口).
You could use a boost::iostreams::array_sink (wrapped with stream to give it an std::ostream interface) for that.
这篇关于直接提升序列化到字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:直接提升序列化到字符数组
基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
