std::strstream::str
From cppreference.com
char* str(); |
(deprecated in C++98) (removed in C++26) |
|
Returns the pointer to the beginning of the buffer, after freezing it. Effectively calls rdbuf()->str()
Parameters
(none)
Return value
Pointer to the beginning of the buffer in the associated std::strstreambuf
Notes
Before a call to str()
that uses the result as a C-string, the stream buffer must be null-terminated. Regular output such as with stream << 1.2 does not store a null terminator, it must be appended explicitly, typically with the manipulator std::ends
After a call to str()
, dynamic streams become frozen. A call to freeze(false)
is required before exiting the scope in which this
strstream
Example
Run this code
#include <iostream> #include <strstream> int main() { std::strstream dyn; // dynamically-allocated output buffer dyn << "Test: " << 1.23; // not adding std::ends to demonstrate append behavior std::cout << "The output stream holds \""; std::cout.write(dyn.str(), dyn.pcount()) << "\"\n"; // the stream is now frozen due to str() dyn << " More text" << std::ends; std::cout << "The output stream holds \""; std::cout.write(dyn.str(), dyn.pcount()) << "\"\n"; dyn.freeze(false); }
Possible output:
The stream holds "Test: 1.23" The stream holds "Test: 1.23 More "
See also
marks the buffer frozen and returns the beginning pointer of the input sequence (public member function of std::strstreambuf ) |