直接提升序列化到字符数组 [英] Direct boost serialization to char array

查看:27
本文介绍了直接提升序列化到字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

这篇关于直接提升序列化到字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆