从 char* 获取 istream
我有一个 char* 和从库接收的数据长度,我需要将数据传递给采用 istream 的函数。
我知道我可以创建一个字符串流,但这将复制所有数据。而且,数据肯定会有 0,因为它是一个 zip 文件,并且创建字符串流将获取数据,直到我认为第一个 0。
有没有办法从 char* 及其大小创建 istream 而无需复制所有数据?
I have a char* and the data length that I'm receiving from a library, and I need to pass the data to a function that takes an istream.
I know I can create a stringstream but that will copy all the data. And also, the data will surely have 0s since it's a zip file, and creating a stringstream will take the data until the first 0 I think.
Is there any way to create an istream from a char* and it's size without copying all the data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是在网络上找到的一个未弃用的方法,您是否派生了自己的
std::streambuf
类,但很简单并且似乎有效:哪个输出:
Here's a non-deprecated method found on the web, has you derive your own
std::streambuf
class, but easy and seems to work:Which outputs:
使用 Boost 的未弃用解决方案:
或更简单:
A non deprecated solution using Boost:
or even simpler:
唯一(简单)的可移植方法包括进行复制:
事实上,这可能会复制数据两次,一次用于创建
string
,一次用于创建istringstream
。 (也许 C++11 可以通过移动构造函数避免其中一个副本;我不确定。)但是,如果您幸运的话,您的 C++ 实现将允许您执行此操作:
在 GNU C++ 下(并且我相信,其他一些实现),这将创建字符串流而不复制数据。但这是根据规范的“实现定义”行为。 (另请参阅此问题。)
通过包含
len
参数,您可以确保这两个都具有空字符没有问题。实现您想要的操作的唯一可移植方法是实现您自己的 stringbuf 子类并使用它来初始化 stringstream。不适合胆小的人。
The only (simple) portable way includes making the copy:
In fact, this is likely to copy the data twice, once to create the
string
and once to create theistringstream
. (Maybe C++11 can avoid one of the copies via a move constructor; I am not sure.)However, if you are lucky, your C++ implementation will let you do this:
Under GNU C++ (and, I believe, some other implementations), this will create the stringstream without copying the data. But this is "implementation-defined" behavior according to the spec. (See also this question.)
By including the
len
parameter, you ensure that both of these will have no problem with null characters.The only portable way to do what you want is to implement your own subclass of
stringbuf
and use it to initialize the stringstream. Not for the faint of heart.我需要一个支持
tellg
和seekg
并且不需要 boost 的解决方案。char_array_buffer
来自编写自定义流缓冲区 (std::streambuf) 的初学者指南 提供了一个起点。byte_array_buffer.h:
byte_array_buffer.cpp:
I needed a solution that supports
tellg
andseekg
and didn't require boost.char_array_buffer
from A beginner's guide to writing a custom stream buffer (std::streambuf) gave a got starting point.byte_array_buffer.h:
byte_array_buffer.cpp:
支持tellg和seekg的已接受答案的扩展:
此类的用法保持不变。
An extension to the accepted answer that supports tellg and seekg:
Usage of this class stays the same.
您尝试过 std::istrstream 吗?
http://stdcxx.apache.org/doc/stdlibref/istrstream.html
从技术上讲,我认为它已被弃用,但仍然是标准的一部分。
Have you tried std::istrstream?
http://stdcxx.apache.org/doc/stdlibref/istrstream.html
Technically, I think that it is deprecated, but still part of the standard.
尝试 Boost.Iostreams 数组源和接收器类。
http://www.boost.org/doc/libs /1_47_0/libs/iostreams/doc/index.html
Try the Boost.Iostreams array source and sink classes.
http://www.boost.org/doc/libs/1_47_0/libs/iostreams/doc/index.html