- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试读取一个文件。它应该是内存映射的性能。我想使用 iostreams 的 boost 过滤器链来轻松包含 zip、bzip2 和 gzip 解压缩。
我尝试采用Using boost::iostreams mapped_file_source and filtering_streambuf to decompress file中提出的解决方案.
我的问题:当我尝试在流上执行 seek()
时,出现异常:“无随机访问”。这很奇怪,因为从文档中我了解到我可以使用 std::istream
作为接口(interface)。
我想出了以下代码(also on 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 有关,但 boost iostream 缓冲区似乎默认为不可搜索。我误解了 boost 文档。
解决方法如下:
替换:
io::filtering_istreambuf filteredInputStream;
通过
io::filtering_streambuf<io::input_seekable> filteredInputStream;
在 coliru 上直播: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++ - Seek 不适用于使用 boost filtering_istreambuf 初始化的 std::istream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34415054/
如果我们查看 istream 和 ostream 的文档,我们有以下功能: istream& seekg ( streampos pos ); istream& seekg ( streamoff o
如果我想实现 seek() 来完成 SeekableIterator 接口(interface),如果搜索的位置无效,我是否应该在内部恢复到旧位置?像这样: public function seek(
python documentation说: In text files (those opened without a b in the mode string), only seeks relat
我正在编写一个 c++ 方法,需要更新打开文件 (ofstream) 中的一些字符。 该方法获取一个映射作为输入,其中键是一个偏移量(文件中的位置),值是一个字符。 代码示例 typedef map
我注意到,当我做这样的事情时: with open('testfile', 'wb') as fl: fl.seek(2048*512) fl.write(b'aaaaa') 无论我的
我正在开发一个 android 应用程序,我想在状态栏中添加音量控制。我可以在状态栏中添加一个搜索栏吗? 我使用过 NotificationManager,我发现在通知栏中添加动画很困难。 但是任何人
我注意到,当我做这样的事情时: with open('testfile', 'wb') as fl: fl.seek(2048*512) fl.write(b'aaaaa') 无论我的
Python seek()函数:设定读写位置 可以使用 seek() 函数调整当前读写文件的位置。例如,我们从文件头部读取了 1000 个字节,处理这 1000 个字节后发现还有一个很重要的信息在文件
我在 Delphi 2006 中使用 TFileStream。当我使用超出范围的偏移量调用 TFileStream.Seek 时,我得到不同的返回值。当我寻找低于流开头的位置时,该函数返回 -1,如果
我们刚刚在 Delphi 2007 中遇到了 TFileStream.Seek 的一种特殊行为(实际上该方法是从 THandleStream 继承的): 您可以在没有错误的情况下查找文件末尾之外的内容
这是取自 https://forums.embarcadero.com/message.jspa?messageID=219481 的代码片段 if FileExists(dstFile) then
我有一个电子表格,其中包含大量输入,但只有一个输出(价格)。 我想让 Excel 自动更新其中一个输入(称为输入 1),以便在调整其他输入的同时保持价格不变。 我知道“目标搜索”可以做到这一点,但我不
根据 API ,这些是事实: seek(long bytePosition)方法简单来说就是将指针移动到用 bytePosition 指定的位置参数。 当 bytePosition大于文件长度,则文件
我们正在使用 StreamingKit (https://github.com/tumtumtum/StreamingKit) 从流式 m4a 音频源列表中播放,用户可以在这些音频源之间自由来回移动。
我有一个程序可以替换文件中的内容。但是却导致了IO异常,我不知道我哪里逻辑错了? 代码如下: import java.io.File; import java.io.RandomAccessFile;
我想从Excel中导出数据 我尝试下面的代码 def exel_all_attendance(request,course_id): all_submit_attendance = Submi
我正在尝试使用 FileStream。寻求快速跳转到一行并阅读它。 但是,我没有得到正确的结果。我试图看这个有一段时间了,但不明白我做错了什么。 环境: 操作系统:Windows 7 架构:.NET
我正在处理大文件,从 10Gb 开始。我正在将文件的各个部分加载到内存中进行处理。以下代码适用于较小的文件 (700Mb) byte[] byteArr = new byte[layerPixelC
我有一个 for 循环,它被设计为接受一个目标数字,并循环遍历堆栈,直到它在堆栈 [target] 中找到数据并且它应该返回该数据。 不幸的是,我只能让它返回它所在的数组的位置或一串随机数字(随着我的
我创建了一个包含以下条目的文件,由 file.read() 返回 'abcd\nefgh\n1234\nijkl\n5678\n\nend' 我现在打开要阅读的文件,使用“f”作为处理程序。f.rea
我是一名优秀的程序员,十分优秀!