ios::rdbuf


public member function
streambuf* rdbuf ( ) const;
streambuf* rdbuf ( streambuf* sb );

Get/set the associated stream buffer

The first syntax returns the stream buffer object associated with the stream.

The second syntax associates the stream with sb and returns the stream buffer object previously associated with the stream. In this case, the buffer's control state is set to goodbit as if a call to member clear().

Parameters

sb
A pointer to the stream buffer object to associate the stream with.


Return Value

A pointer to the stream buffer object associated with the stream before the call.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// redirecting cout's output
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  streambuf *psbuf, *backup;
  ofstream filestr;
  filestr.open ("test.txt");

  backup = cout.rdbuf();     // back up cout's streambuf

  psbuf = filestr.rdbuf();   // get file's streambuf
  cout.rdbuf(psbuf);         // assign streambuf to cout

  cout << "This is written to the file";
  
  cout.rdbuf(backup);        // restore cout's original streambuf

  filestr.close();

  return 0;
}


This example uses both function syntaxes to first get a pointer to a file's streambuf object and later assigns it to cout.

Basic template member declaration

( basic_ios<charT,traits> )
1
2
basic_streambuf<charT,traits> * rdbuf () const;
basic_streambuf<charT,traits> * rdbuf ( basic_streambuf<charT,traits> sb );


See also