ostrstream class — Output character array streams
class ostrstream: public ostream { public: ostrstream( ); ostrstream(char* str, int n, ios_base::openmode mode = ios_base::out); strstreambuf* rdbuf( ) const; void freeze(bool flag = true); char* str( ); int pcount( ) const; };
The ostrstream
class
represents an output string stream. You can provide a character
array, and the stream contents are written to that array. Another
typical usage is to construct an ostrstream
with no argument and let the
string stream allocate the string as you write to the stream. Then
call str( )
to obtain the
resulting character array. Once you call str( )
, the stream is
frozen and cannot be modified. The pointer
returned from str( )
remains
valid until the ostrstream
object
is destroyed or until you thaw the stream to
allow writing again.
The following are the methods of ostrstream
:
ostrstream
( )
Initializes an empty output string stream by
constructing an internal strstreambuf
object and passing the
address of the string buffer to the base-class constructor for
ostream
.
ostrstream
(char* str, int n, ios_base::openmode
mode
=
ios_base::out)
Initializes a string stream with str
as the initial string contents
by constructing an internal strstreambuf
object and passing the
address of the buffer to the base-class constructor for
ostream
. If the ios_base::app
bit is set in mode
, the buffer is constructed like
this:
strstreambuf(str, n, str + std::strlen(str));
If the ios_base::app
bit is clear in mode
, the
buffer is constructed like this:
strstreambuf(str, n, str);
void
freeze
(bool flag =
true)
Freezes or thaws the buffer by calling rdbuf( )->freeze(flag)
.
strstreambuf*
rdbuf
( )
const
Returns a pointer to the internal strstreambuf
object.
char*
str
( )
Returns a pointer to the buffer's character array, that
is, rdbuf( )->str(
)
.
int
pcount
( )
const
Returns the number of bytes in the output buffer by
calling rdbuf( )->pcount(
)
.
istrstream class,
strstream class, strstreambuf class, ostream
in <ostream>
, ostringstream
in <sstream>