Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将std::istream直接连接到C数组/无符号字符*_C++_Istream - Fatal编程技术网

C++ 将std::istream直接连接到C数组/无符号字符*

C++ 将std::istream直接连接到C数组/无符号字符*,c++,istream,C++,Istream,我目前正在处理一个自定义缓冲区类,它在内部以经典的C数组(unsignedchar[])携带数据 为了对缓冲区进行更舒适的读/写访问,我正在寻找一种方法来构造一个std::istream对象,该对象直接连接到POD内容。。。又名C阵列内存。目标是使用所有std::stream格式化程序,实际数据“lorem ipsum”应该直接写入缓冲区。比如说: std::istream QuirkyBuffer::getIStream() { return std::istream(this->ptr

我目前正在处理一个自定义缓冲区类,它在内部以经典的C数组(unsignedchar[])携带数据

为了对缓冲区进行更舒适的读/写访问,我正在寻找一种方法来构造一个std::istream对象,该对象直接连接到POD内容。。。又名C阵列内存。目标是使用所有std::stream格式化程序,实际数据“lorem ipsum”应该直接写入缓冲区。比如说:

std::istream QuirkyBuffer::getIStream() { return std::istream(this->ptr, this->size); }

QuirkyBuffer d;
auto is = d.getIStream();
"lorem ipsum" >> is;

可以这样做吗?

您可以为此使用
std::ostream
。它被弃用了,但鉴于它的实用性,我无法想象它会很快消失


否则,编写自己的
omemstream

非常简单。istream不是问题,问题在于编写一个streambuffer,因为ifstream只是从istream派生的一个类,包含一个streambuffer和一些粘合代码。现在,为了编写streambuffer,您需要重写私有虚拟输入函数。我认为underflow()和uflow()甚至足够了,但是使用这些关键字,您应该能够自己找到所需的信息

顺便说一句:流是不可复制的,除非在C++11中改变了这一点,所以按值返回是不可能的