cout, clog and cerr are pre-defined objects defined in ostream.
The iostream class inherits from both ostream and istream
cout and clog are buffered , that means all output is temporarily stored in a buffer and then dumps to screen.
cerr is an unbuffered object , that means all output goes immediately to the output device.
In C++ program , we can redirect the cout by the buffer area , but we can't redirect cerr by the same method.
for example:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
cout << "the first row" << endl;
streambuf* cout_buffer = cout.rdbuf(); // save the original cout bufffer
ofstream file("redirect.txt");
cout.rdbuf (file.rdbuf()); //redirect the cout buffer to file buffer
cout << "the last row" << endl;
cout.rdbuf (cout_buffer); // recover the cout buffer
return 0;
}