Seek 不适用于使用 boost filtering_istreambuf初始化的 std::istream
Seek doesn't work on std::istream initialized with boost filtering_istreambuf
我试图读取文件。它应该是内存映射以提高性能。我想使用 iostream 的提升过滤器链轻松包含 zip、bzip2 和 gzip 解压缩。
我尝试采用使用 boost::iostreams mapped_file_source 和 filtering_streambuf 解压缩文件中提出的解决方案。
我的问题:当我尝试在流上seek()
时,我得到一个异常:"没有随机访问"。这很奇怪,因为从文档中我了解到我可以将std::istream
用作接口。
我想出了以下代码(也在coliru上(:
#include <iostream>
#include <stdio.h>
#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
// and later also include ...
//#include <boost/iostreams/filter/gzip.hpp>
//#include <boost/iostreams/filter/bzip2.hpp>
int main()
{
namespace io = boost::iostreams;
io::mapped_file_source inputDevice; // the device to read from (file)
io::stream<io::mapped_file_source> mappedFileStream; // the memory mapped file stream
io::filtering_istreambuf filteredInputStream; // the source file (stream), together with a chain of filters and/or decompressors
inputDevice = io::mapped_file_source("main.cpp");
mappedFileStream.open(inputDevice); // open device as a readable stream
// add optional filter/decoder/decompressor
//filteredInputStream.push(io::bzip2_decompressor());
//filteredInputStream.push(io::zlib_decompressor());
//filteredInputStream.push(io::gzip_decompressor());
//filteredInputStream.push(io::base64_decoder());
filteredInputStream.push(mappedFileStream); //finally add the readable stream
// now we have a device that is streamed and the contents are filtered the resulting stream can be used like a std::iostream. (in theory)
std::istream inputStream(&filteredInputStream);
std::cout << inputStream.get() << std::endl;
std::cout << inputStream.get() << std::endl;
//fatal error: class boost::exception_detail::clone_impl<struct boost::exception_detail::error_info_injector<class std::ios_base::failure> >: no random access
filteredInputStream.pubseekoff(0, std::ios_base::beg, std::ios_base::in | std::ios_base::out);
inputStream.seekg(0); // fatal error: class boost::exception_detail::clone_impl<struct boost::exception_detail::error_info_injector<class std::ios_base::failure> >: no random access
std::cout << inputStream.get() << std::endl;
std::cout << inputStream.get() << std::endl;
return 0;
}
在撰写问题时,我在这里找到了解决方案。
boost::iostreams, gzip files and tellg
这个问题与 gzip 有关,但提升 iostream 缓冲区似乎默认为不可寻。我误解了提升文档。
所以这是解决方案:
取代:
io::filtering_istreambuf filteredInputStream;
由
io::filtering_streambuf<io::input_seekable> filteredInputStream;
住在大肠杆菌上:http://coliru.stacked-crooked.com/a/aff637be181a27da
#include <iostream>
#include <stdio.h>
#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
// and later also include ...
//#include <boost/iostreams/filter/gzip.hpp>
//#include <boost/iostreams/filter/bzip2.hpp>
int main()
{
namespace io = boost::iostreams;
io::mapped_file_source inputDevice; // the device to read from (file)
io::stream<io::mapped_file_source> mappedFileStream; // the memory mapped file stream
io::filtering_streambuf<io::input_seekable> filteredInputStream; // the source file (stream), together with a chain of filters and/or decompressors
//io::filtering_istreambuf filteredInputStream; // wrong. Defaults to non-seekable. Throws exception on seek or tell
inputDevice = io::mapped_file_source("main.cpp");
mappedFileStream.open(inputDevice); // open device as a readable stream
// add optional filter/decoder/decompressor
//filteredInputStream.push(io::bzip2_decompressor());
//filteredInputStream.push(io::zlib_decompressor());
//filteredInputStream.push(io::gzip_decompressor());
//filteredInputStream.push(io::base64_decoder());
filteredInputStream.push(mappedFileStream); //finally add the readable stream
// now we have a device that is streamed and the contents are filtered the resulting stream can be used like a std::iostream. (in theory)
std::istream inputStream(&filteredInputStream);
std::cout << inputStream.get() << std::endl;
std::cout << inputStream.get() << std::endl;
filteredInputStream.pubseekoff(0, std::ios_base::beg, std::ios_base::in | std::ios_base::out);
inputStream.seekg(0);
std::cout << inputStream.get() << std::endl;
std::cout << inputStream.get() << std::endl;
return 0;
}
相关文章:
- 是否可以初始化不可复制类型的成员变量(或基类)
- C++使用整数的压缩数组初始化对象
- C++初始化基类
- 多成员Constexpr结构初始化
- 复制列表初始化的隐式转换的等级是多少
- 内联映射初始化的动态atexit析构函数崩溃
- 如何在C++中初始化嵌套类中的2个memeber
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 没有用于初始化C++中的变量模板的匹配构造函数
- 在未初始化映射的情况下,将值插入到映射的映射中
- C++成员初始化
- 为什么在C++中首先初始化成员类
- 同时具有"聚合初始化"和"模板推导"
- 初始化具有非默认构造函数的std::数组项的更好方法
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 在C和C++中初始化结构中的数组
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 在函数内部的声明中初始化数组,并在外部使用它
- 继承:构造函数,初始化C++11中基类的类C数组成员