1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
// redirecting cout's output thrrough its stream buffer
#include <iostream> // std::streambuf, std::cout
#include <fstream> // std::ofstream
int main () {
std::streambuf *psbuf, *backup;
std::ofstream filestr;
filestr.open ("test.txt");
backup = std::cout.rdbuf(); // back up cout's streambuf
psbuf = filestr.rdbuf(); // get file's streambuf
std::cout.rdbuf(psbuf); // assign streambuf to cout
std::cout << "This is written to the file";
std::cout.rdbuf(backup); // restore cout's original streambuf
filestr.close();
return 0;
}
|