// This is quick and dirty and is just meant as an example of what you could do.
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
char buffer[1024];
size_t const buffer_size = sizeof(buffer);
struct my_stringbuf : stringbuf // sub-class a stringstream buffer
{
streambuf* setbuf ( char* s, streamsize n)
{
s_ = s;
n_ = n;
pos_ = 0;
return this;
}
typedef char_traits<char> traits;
typedef traits::int_type int_type;
int_type overflow ( int_type c = traits::eof() )
{
if(c != traits::eof() && pos_ < n_)
*(s_ + pos_++) = (traits::char_type) c;
return c;
}
char* s_;
streamsize n_;
size_t pos_;
};
int main()
{
my_stringbuf mybuf;
mybuf.setbuf(buffer, buffer_size);
iostream io(&mybuf);
io << "foo\n";
cout << buffer;
}
http://www.cplusplus.com/reference/iostream/stringstream/
Apart from the fact you will find most compilers have no (or limited) support for strstream, it is also a fundamentally flawed class (it has many buffer ownership issues that can lead to hard to diagnose and fix issues)