Skip to content

Instantly share code, notes, and snippets.

@johnsyweb
Created January 20, 2011 22:13
Show Gist options
  • Save johnsyweb/788793 to your computer and use it in GitHub Desktop.
Save johnsyweb/788793 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
class scoped_cout_redirector
{
public:
scoped_cout_redirector(const std::string& filename)
:backup_(std::cout.rdbuf())
,filestr_(filename.c_str())
,sbuf_(filestr_.rdbuf())
{
std::cout.rdbuf(sbuf_);
}
~scoped_cout_redirector()
{
std::cout.rdbuf(backup_);
}
private:
scoped_cout_redirector();
scoped_cout_redirector(const scoped_cout_redirector& copy);
scoped_cout_redirector& operator =(const scoped_cout_redirector& assign);
std::streambuf* backup_;
std::ofstream filestr_;
std::streambuf* sbuf_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment