I have a program in c++, during the program i use :
static ofstream s_outF(file.c_str());
if (!s_outF)
{
cerr << "ERROR : could not open file " << file << endl;
exit(EXIT_FAILURE);
}
cout.rdbuf(s_outF.rdbuf());
Meaning i redirect my cout to a file.
What would be the easiest way to return the cout back to the standard output?
thanks.
Save the old streambuf before you change
cout‘s streambuf :You can write a
restorerto do that automatically as:Now use it based on scope as:
The last
coutwould output tostdouteven ifconditionistrueand theifblock is executed.