I need to redirect a copy of std :: cout to a file. That is, I need to see the output in the console and in the file. If I use this:
// redirecting cout output #include <iostream> #include <fstream> using namespace std; int main () { streambuf *psbuf, *backup; ofstream filestr; filestr.open ("c:\\temp\\test.txt"); backup = cout.rdbuf(); // back up cout streambuf psbuf = filestr.rdbuf(); // get file streambuf cout.rdbuf(psbuf); // assign streambuf to cout cout << "This is written to the file"; cout.rdbuf(backup); // restore cout original streambuf filestr.close(); return 0; }
then I write a line to a file, but I don’t see anything in the console. How can i do this?
source share