You can write your own streambuf class that works directly in your memory:
class membuf : public std::streambuf { public: membuf( char * mem, size_t size ) { this->setp( mem, mem + size ); this->setg( mem, 0, mem + size ); } int_type overflow( int_type charval = traits_type::eof() ) { return traits_type::eof(); } int_type underflow( void ) { return traits_type::eof(); } int sync( void ) { return 0; } };
Use this class:
membuf buf(address,size); ostream os(&buf); istream is(&buf); oss << "Write to the buffer";
source share