Skip to content

Instantly share code, notes, and snippets.

@atcruice
Created May 10, 2014 12:08
Show Gist options
  • Save atcruice/8a3b0ab197bd5931a48c to your computer and use it in GitHub Desktop.
Save atcruice/8a3b0ab197bd5931a48c to your computer and use it in GitHub Desktop.
C++ stream redirection for testing
// save current buffer
streambuf* coutBak = cout.rdbuf();
// setup temp stream
stringstream coutBuf;
// redirect to temp
cout.rdbuf(coutBuf.rdbuf());
// test against contents of coutBuf.str()
// reset
coutBuf.str("");
// test again
// restore stream
cout.rdbuf(coutBak);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment