我找不到任何现成的东西,所以我想出了:
class membuf : public basic_streambuf<char>
{
public:
membuf(char* p, size_t n) {
setg(p, p, p + n);
setp(p, p + n);
}
}
用法:
char *mybuffer;
size_t length;
// ... allocate "mybuffer", put data into it, set "length"
membuf mb(mybuffer, length);
istream reader(&mb);
// use "reader"
我知道stringstream
,但它似乎无法使用给定长度的二进制数据。
我在这里发明了自己的轮子吗?
修改
答案 0 :(得分:28)
我假设您的输入数据是二进制(而不是文本),并且您希望从中提取二进制数据块。所有这些都没有复制输入数据。
您可以将boost::iostreams::basic_array_source
和boost::iostreams::stream_buffer
(来自Boost.Iostreams)与boost::archive::binary_iarchive
(来自Boost.Serialization)结合使用,以便能够使用方便的提取&gt;&gt;运算符读取二进制数据块。
#include <stdint.h>
#include <iostream>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/archive/binary_iarchive.hpp>
int main()
{
uint16_t data[] = {1234, 5678};
char* dataPtr = (char*)&data;
typedef boost::iostreams::basic_array_source<char> Device;
boost::iostreams::stream_buffer<Device> buffer(dataPtr, sizeof(data));
boost::archive::binary_iarchive archive(buffer, boost::archive::no_header);
uint16_t word1, word2;
archive >> word1 >> word2;
std::cout << word1 << "," << word2 << std::endl;
return 0;
}
在AMD64上使用GCC 4.4.1,它输出:
1234,5678
Boost.Serialization非常强大,并且知道如何序列化所有基本类型,字符串甚至STL容器。您可以轻松地使类型可序列化。请参阅文档。隐藏在Boost.Serialization源中的某个地方是一个便携式二进制存档的示例,它知道如何为机器的字节顺序执行正确的交换。这对你也很有用。
如果您不需要Boost.Serialization的优点并且乐于以fread()方式读取二进制数据,则可以更简单的方式使用basic_array_source
:
#include <stdint.h>
#include <iostream>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
int main()
{
uint16_t data[] = {1234, 5678};
char* dataPtr = (char*)&data;
typedef boost::iostreams::basic_array_source<char> Device;
boost::iostreams::stream<Device> stream(dataPtr, sizeof(data));
uint16_t word1, word2;
stream.read((char*)&word1, sizeof(word1));
stream.read((char*)&word2, sizeof(word2));
std::cout << word1 << "," << word2 << std::endl;
return 0;
}
我用这个程序得到了相同的输出。
答案 1 :(得分:5)
我不确定你需要什么,但这样做你想要的吗?
char *mybuffer;
size_t length;
// allocate, fill, set length, as before
std::string data(mybuffer, length);
std::istringstream mb(data);
//use mb
答案 2 :(得分:4)
标准流缓冲区具有此功能 创建一个流。获取它的缓冲区然后覆盖它。
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
// Your imaginary buffer
char buffer[] = "A large buffer we don't want to copy but use in a stream";
// An ordinary stream.
std::stringstream str;
// Get the streams buffer object. Reset the actual buffer being used.
str.rdbuf()->pubsetbuf(buffer,sizeof(buffer));
std::copy(std::istreambuf_iterator<char>(str),
std::istreambuf_iterator<char>(),
std::ostream_iterator<char>(std::cout)
);
}
答案 3 :(得分:2)
提问者想要一些不会复制数据的东西,他的解决方案运行正常。我的贡献是清理它一点,所以你可以创建一个单独的对象,它是内存中数据的输入流。我已经测试了这个并且它可以工作。
class MemoryInputStream: public std::istream
{
public:
MemoryInputStream(const uint8_t* aData,size_t aLength):
std::istream(&m_buffer),
m_buffer(aData,aLength)
{
rdbuf(&m_buffer); // reset the buffer after it has been properly constructed
}
private:
class MemoryBuffer: public std::basic_streambuf<char>
{
public:
MemoryBuffer(const uint8_t* aData,size_t aLength)
{
setg((char*)aData,(char*)aData,(char*)aData + aLength);
}
};
MemoryBuffer m_buffer;
};