Namespaces
Variants
Views
Actions

std::strstream::str

From cppreference.com
< cpp‎ | io‎ | strstream
Revision as of 15:36, 9 December 2012 by Cubbi (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
char* str();

Returns the pointer to the beginning of the buffer, after freezing it. Effectively calls rdbuf()->str()

Contents

Notes

After a call to str(), dynamic streams become frozen. A call to freeze(false) is required before exiting the scope in which this strstream 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.

Parameters

(none)

Return value

Pointer to the beginning of the buffer in the associated std::strsteambuf or NULL if no buffer is available.

Example

#include <strstream>
#include <iostream>
 
int main()
{
    std::strstream dyn; // dynamically-allocated output buffer
    dyn << "Test: " << 1.23;
    std::cout << "The output stream holds \"" << dyn.str() << "\"\n";
 
    dyn << "Test: " << 1.23;
    std::cout << "The output stream holds \"" << dyn.str() << "\"\n";
    // the stream is now frozen due to str()
    dyn << " More text";
    std::cout << "The output stream holds \"" << dyn.str() << "\"\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) [edit]