Routes stream to specified buffer

Developer Command Prompt for VS 2019

  1. cl basic_ios_rdbuf.cpp /EHsc

MSDN Source Code

  1. #include <ios>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. int main( ) 
  6. {
  7.    using namespace std;
  8.    ofstream file( "rdbuf.txt" );
  9.    streambuf *x = cout.rdbuf( file.rdbuf( ) );
  10.    cout << "test" << endl; // Goes to file
  11.    cout.rdbuf(x);
  12.    cout << "test2" << endl; //normal
  13. }

Leave a Reply

Your email address will not be published. Required fields are marked *