来自 istream 的子流
假设我有一个 ifstream
,它表示一个包含许多聚合在一起的子文件的大文件。我希望能够从较大的 ifstream
(给定大小和offest)创建一个代表文件一部分的“子”istream
,以便其他代码可以从中读取子流就好像它是一个独立的istream
。
关于我如何实现这一目标有什么想法吗?
编辑 - 我宁愿避免提升。
Suppose I have an ifstream
which represents a large file containing lots of sub-files aggregated together. I want to be able to create a "sub" istream
from the larger ifstream
(given a size and offest) representing a part of the file so other code can read from that substream as if it was an independent istream
.
Any ideas on how I might accomplish this?
EDIT
- I would prefer to avoid boost.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个streambuf“过滤器”的示例,它从指定位置开始读取包含的streambuf 并读取指定大小。您创建
substreambuf
,传入原始streambuf
,然后substreambuf
转换访问权限,以便从底层中的所需位置读取所有内容流缓冲区。
从
underflow
和uflow
调用sgetc
和snextc
所涉及的大部分开销应该被优化掉。许多提取运算符逐字节工作,因此除了维护小节内的读取位置和检查小节末尾之外,不应有额外的开销。当然,使用此类读取大块数据的效率会较低(尽管这可以修复)。这仍然需要改进,例如测试请求的位置是否位于底层
streambuf
内。它可以像这样使用
这受到过滤Streambufs的启发
This is an example of a streambuf "filter" that reads from a contained streambuf starting at a specified location and reading up to a specified size. You create
substreambuf
, passing your originalstreambuf
in andsubstreambuf
then translates access so that everything is read from the desired location in the underlyingstreambuf
.Most of the overhead involved in calling
sgetc
andsnextc
fromunderflow
anduflow
should optimize away. Many extraction operators work byte by byte, so there should not be additional overhead beyond maintaining the read position within the subsection and checking for the end of the subsection. Of course, reading large chunks of data will be less efficient with this class (although that could be fixed).This still needs improvements like testing that the requested location is within the underlying
streambuf
.It can be used like this
This was inspired by Filtering Streambufs
我已经使用 Boost.Iostreams< 完成了类似的事情/a> 库。查看教程|书写设备。这个想法是创建一个实现低级接口(读/写/查找)的“设备”类,然后使用设备类实例化 istream/ostream 派生类来执行实际的 I/O。
I've done something like this using the Boost.Iostreams library. Look under Tutorial|Writing Devices. The idea is to create a "device" class which implements the low-level interface (read/write/seek) and then instantiate an istream/ostream derived class using your device class to do the actual I/O.
所有 iostream 都将大部分自定义逻辑放入其
streambuf
专业化中。fstream
(或basic_fstream
)使用file_buf
实例初始化istream
。与stringstream
(stringbuf
) 相同。如果您想滚动自己的子流流,可以通过在父流方面实现您自己的streambuf
来实现。All iostreams put most of their custom logic in their
streambuf
specializations.fstream
(orbasic_fstream
) initializesistream
with an instance offile_buf
. Same forstringstream
(stringbuf
). If you want to roll your own substream stream, you can do it by implementing your ownstreambuf
in terms of a parent stream.只是一个小想法:如果您可以控制代码的客户端(即使用输入流的部分),我建议您修改它以接受两个附加参数,如下所示:
可以变成:
Just a little idea : If you have control over the client side of the code (i.e. the part that uses the input stream), I suggest you modify it to accept two additional parameters, like illustrated below :
Can become :