Victor Bazarov said:
The simplest way I know is to use std::stringstream.
stringstream is useful a lot of the time, but it has the following
limitations (both reasonable, in context):
1. You can't specify that the stream access a pre-existing block of
memory,
2. When you're done performing i/o you can't get direct access to the
underlying memory -- you have to settle for a copy.
A library up for review at boost (by Darlye Walker) offers a
collection of streams and stream buffers for accessing arrays
directly, with this usage:
char buf[1000];
boost::io:
ointerstream ps(buf, buf + 1000);
Here buf need not be on the stack -- it could just as well be
dynamically allocated. Of course, unlike with a std::stringstream, the
user has to be careful not to write past the end of the array.
I've submitted a slightly more general iostreams libray which allows
the creation of a pointerstream as follows:
typedef boost::io::stream_facade<array_resource>
pointerstream;
char buf[1000];
pointerstream ps(buf, buf + 1000);
You can also access mmap'd files as follows:
typedef boost::io::stream_facade<mapped_file_resource>
mapped_fstream;
mapped_fstream ms("hello.txt");
Docs are here:
more_io (Daryle Walker)
http://tinyurl.com/5d9xt
iostreams (Jonathan Turkanis)
http://tinyurl.com/4zfmt
Jonathan