Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何序列化为原始内存块? 使用升压、将C++对象序列化为文件是非常容易的 std::ofstream ofile( lpszFileName ); boost::archive::text_oarchive oa(ofile); oa << m_rgPoints; 文件流的std::of(lpszFileName); boost::archive::text\u oarchive oa(ofile); oa_C++_Serialization_Boost - Fatal编程技术网

如何序列化为原始内存块? 使用升压、将C++对象序列化为文件是非常容易的 std::ofstream ofile( lpszFileName ); boost::archive::text_oarchive oa(ofile); oa << m_rgPoints; 文件流的std::of(lpszFileName); boost::archive::text\u oarchive oa(ofile); oa

如何序列化为原始内存块? 使用升压、将C++对象序列化为文件是非常容易的 std::ofstream ofile( lpszFileName ); boost::archive::text_oarchive oa(ofile); oa << m_rgPoints; 文件流的std::of(lpszFileName); boost::archive::text\u oarchive oa(ofile); oa,c++,serialization,boost,C++,Serialization,Boost,如果我知道您需要二进制序列化boost::archive::binary\u oarchive。然后您可以从流中复制数据。根据James Kanze的评论编辑: 您可以序列化为std::ostringstream: std::ostringstream oss; boost::archive::text_oarchive oa(oss); oa << m_rgPoints; std::ostringstream oss; boost::archive::text_oarchive

如果我知道您需要二进制序列化
boost::archive::binary\u oarchive
。然后您可以从流中复制数据。

根据James Kanze的评论编辑:

您可以序列化为
std::ostringstream

std::ostringstream oss;
boost::archive::text_oarchive oa(oss);
oa << m_rgPoints;
std::ostringstream oss;
boost::archive::text_oarchive oa(oss);

oa您可以编写自己的
streambuf
类,它直接作用于您的内存:

class membuf : public std::streambuf
{
public:
  membuf( char * mem, size_t size )
  {
    this->setp( mem, mem + size );
    this->setg( mem, 0, mem + size );
  }
  int_type overflow( int_type charval = traits_type::eof() )
  {
    return traits_type::eof();
  }
  int_type underflow( void )
  {
    return traits_type::eof();
  }
  int sync( void )
  {
    return 0;
  }
};
使用此类:

  membuf buf(address,size);
  ostream os(&buf);
  istream is(&buf);

  oss << "Write to the buffer";
membuf(地址、大小);
ostream操作系统&buf;
istream is(&buf);

oss实际上有一个二进制原始数据的序列化包装器

您可以像这样使用它:

// buf is a pointer to a raw block of memory, size its size
// oa is a boost archive
boost::serialization::binary_object buf_wrap(buf, size);
oa << buf_wrap

然而,这意味着一个副本。

仅用于记录:
ios\u base::binary
标志被
stringbuf
忽略;它只与
filebuf
ifstream
流的
相关)。既然你只是在写,ostringstream不是更合适吗?我不知道二进制文件只与filebuf相关。谢谢你,当然ostringstream会更合适。如果你不需要事先指定缓冲区大小,这会更合适。但问题可能暗示这是一项要求。
// buf is a pointer to a raw block of memory, size its size
// oa is a boost archive
auto start_buf = reinterpret_cast<byte *>(buf);
std::vector<std::byte> vec(start_buf, start_buf + size);
oa << vec;