| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Member functions | ||||
strstream::str | ||||
char* str(); | (deprecated in C++98) (removed in C++26) | |
Returns the pointer to the beginning of the buffer, after freezing it. Effectively callsrdbuf()->str().
Contents |
(none)
Pointer to the beginning of the buffer in the associatedstd::strstreambuf or a null pointer if no buffer is available.
Before a call tostr() that uses the result as a C-string, the stream buffer must be null-terminated. Regular output such as withstream<<1.2 does not store a null terminator, it must be appended explicitly, typically with the manipulatorstd::ends.
After a call tostr(), dynamic streams become frozen. A call tofreeze(false) is required before exiting the scope in which thisstrstream object was created. otherwise the destructor will leak memory. Also, additional output to a frozen stream may be truncated once it reaches the end of the allocated buffer, which may leave the buffer not null-terminated.
#include <iostream>#include <strstream> int main(){std::strstream dyn;// dynamically-allocated output buffer dyn<<"Test: "<<1.23;// not adding std::ends to demonstrate append behaviorstd::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 "
| marks the buffer frozen and returns the beginning pointer of the input sequence (public member function of std::strstreambuf)[edit] |