Namespaces
Variants
Views
Actions

std::basic_ios::rdbuf

From cppreference.com
< cpp‎ | io‎ | basic ios
Revision as of 14:23, 10 September 2013 by P12 (Talk | contribs)

std::basic_streambuf<CharT, Traits>* rdbuf() const;
(1)
std::basic_streambuf<CharT, Traits>* rdbuf( std::basic_streambuf<CharT, Traits>* sb );
(2)

Manages the associated stream buffer.

1) Returns the associated stream buffer. If there is no associated stream buffer, returns NULL.
2) Sets the associated stream buffer to sb. The error state of sb is cleared by calling sb.clear(). Returns the associated stream buffer before the operation. If there is no associated stream buffer, returns NULL.

Contents

Parameters

sb - stream buffer to associate to

Return value

The associated stream buffer, or NULL if there was no associated stream buffer.

Exceptions

(none)

Example

#include <iostream>
#include <sstream>
 
int main()
{
    std::ostringstream local;
    auto cout_buff = std::cout.rdbuf(); // save pointer to std::cout buffer
 
    std::cout.rdbuf(local.rdbuf()); // substitute internal std::cout buffer with
        // buffer of 'local' object
 
    // now std::cout work with 'local' buffer
    // you don't see this message
    std::cout << "some message";
 
    // go back to old buffer
    std::cout.rdbuf(cout_buff);
 
    // you will see this message
    std::cout << "back to default buffer\n";
 
    // print 'local' content
    std::cout << "local content: " << local.str() << "\n";
}

Output:

back to default buffer
local content: some message

See also

replaces the rdbuf without clearing its error state
(protected member function) [edit]