c++ - boost filtering_stream和tellp

标签 c++ boost boost-iostreams

我正在尝试使用filtering_streams将某些对象的序列化压缩到array_sink或类似设备中,然后我可以确定压缩输出的长度并将其复制到另一个流,例如文件。但是,在 filtering_ostream 上使用 ostream::tellp 会导致 boost 引发运行时异常。我真的不知道我做错了什么。

using namespace boost::iostreams;

char *buffer = new char[4096*255];
array_sink zipStream(buffer, 4096*255);

filtering_ostream tempOut;
tempOut.push(zlib_compressor());
tempOut.push(zipStream);

column->Serialize(tempOut); // Object::Serialize(ostream&)
tempOut.flush(); // ?
int zipSize = tempOut.tellp();

// Do stuff with zipStream...

最佳答案

问题在于 tellp 是根据底层流缓冲区 pubseekoff 实现的,与当前写入头位置的偏移量为 0(基本上,这只是糟糕的设计)。现在,这里的关键是 zlib_compressor 不能与 output_seekable 接收器一起使用(如文档中所示)。这是相当自然的,因为更改写入头几乎肯定会导致数据损坏。如果您尝试解压缩,也会遇到同样的问题。

关于c++ - boost filtering_stream和tellp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10533170/

相关文章:

c++ - 测量库调用和回调之间的时间

c++ - 是否有用于 Visual C++ 的编译器标志来检查调用 printf() 的类型安全性?

c++ - 我如何使用 Boost.Filesystem 知道文件的类型?

c++ - 如何在不使用 C++11 的情况下使用多个值初始化 boost::mt19937

c++ - boost iostream 映射文件和 boost 进程间映射文件之间有区别吗?

c++ - 为什么mapped_file::data 返回 char* 而不是 void*

c++ - VC++2010中使用Direct3D9和FMOD自由播放音视频

c++ - TCP端口访问和C++

c++ - boost::正则表达式-\bb?

c++ - BOOST.IO流 : trouble to write to bzip2