strstream class — Input and output character array streams
class strstream: public iostream { public: typedef char char_type; typedef typename char_traits<char>::int_type int_type; typedef typename char_traits<char>::pos_type pos_type; typedef typename char_traits<char>::off_type off_type; strstream( ); strstream(char* s, int n, ios_base::openmode mode = ios_base::in|ios_base::out); virtual ~strstream( ); strstreambuf* rdbuf( ) const; void freeze(bool freezefl = true); int pcount( ) const; char* str( ); };
The strstream
class is a
stream class that performs input and output to a character array.
You can start with an empty string and write to the stream, or start
with a string and read from the stream. You can switch between
reading and writing at any time. If you use the default constructor
and write to the stream, the stream buffer grows as needed. Then you
can 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 strstream
:
strstream
( )
Initializes an empty string stream by constructing an
internal strstreambuf
object and passing the address of the string buffer to the
base-class constructor for iostream
.
basic_strstream
(char* str, int n,
ios_base::openmode
mode
=
ios_base::in|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
iostream
. 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,
ostrstream class, strstreambuf class, basic_iostream
in <istream>
, stringstream
in <sstream>