You need to implement a custom streambuf
that you then pass to the istream
constructor. This will allow you to access the array data as any other stream. See the following links for more details:
- std::streambuf
- std::istream
Boost provides the iostreams library for helping with this. In particular, it already provides the array_source
class for wrapping standard arrays. The following code sample illustrates how to accomplish this:
#include <cstdlib>
#include <iostream>
#include <string>
#include <boost/iostreams/stream_buffer.hpp>
#include <boost/iostreams/device/array.hpp>
namespace io = boost::iostreams;
int main(int argc, char* argv[]) {
const char buffer[] = "buffer data\n";
io::array_source src{ buffer, strlen(buffer) };
io::stream_buffer<io::array_source> strbuf{ src };
std::istream stream{ &strbuf };
std::string line;
std::getline(stream, line);
std::cout << line << std::endl;
return EXIT_SUCCESS;
}