| 1 |
#include <iostream> |
| 2 |
#include <fstream> |
| 3 |
using namespace std; |
| 4 |
|
| 5 |
int main(int argc, char **argv) |
| 6 |
{ |
| 7 |
ofstream errlog; // 1 |
| 8 |
streambuf *cerr_buffer = 0; // 2 |
| 9 |
|
| 10 |
if (argc == 2) |
| 11 |
{ |
| 12 |
errlog.open(argv[1]); // 3 |
| 13 |
cerr_buffer = cerr.rdbuf(errlog.rdbuf()); // 4 |
| 14 |
} |
| 15 |
else |
| 16 |
{ |
| 17 |
cerr << "Missing log filename\n"; |
| 18 |
return 1; |
| 19 |
} |
| 20 |
|
| 21 |
cerr << "Several messages to stderr, msg 1\n"; |
| 22 |
cerr << "Several messages to stderr, msg 2\n"; |
| 23 |
|
| 24 |
cout << "Now inspect the contents of " << |
| 25 |
argv[1] << "... [Enter] "; |
| 26 |
cin.get(); // 5 |
| 27 |
|
| 28 |
cerr << "Several messages to stderr, msg 3\n"; |
| 29 |
|
| 30 |
cerr.rdbuf(cerr_buffer); // 6 |
| 31 |
cerr << "Done\n"; // 7 |
| 32 |
} |
| 33 |
/* |
| 34 |
Generated output on file argv[1] |
| 35 |
|
| 36 |
at cin.get(): |
| 37 |
|
| 38 |
Several messages to stderr, msg 1 |
| 39 |
Several messages to stderr, msg 2 |
| 40 |
|
| 41 |
at the end of the program: |
| 42 |
|
| 43 |
Several messages to stderr, msg 1 |
| 44 |
Several messages to stderr, msg 2 |
| 45 |
Several messages to stderr, msg 3 |
| 46 |
*/ |