Defaultly cout will show on the console screen.
We can redirect the output to a file by change the streambuf.
But, sometimes we would like to see the message and message and keep them in log file as well. Can we make the cout put on both screen and file using the same line:
cout << val
I want to put val 'm' to both screen and file. I can do it by:
1 2 3 4 5 6
int m = 10;
ofstream outFile("output.txt");
outFile << m; // put on file
cout << m; // put on screen
//- using this conventional method, two puts operations are conducted separately.
Can I do some streambuf configures, then:
1 2 3 4 5 6 7
int m = 10;
ofstream outFile("output.txt");
//- ? some configuration on streambuf of outFile and cout
cout << m; // put on screen, and screen
//- then this single operation can achieve the same results as previous code.
How can I do the overload if their the arguments are the same, but operations are different.
I got a idea about to redefine a class (STRA), and operates on cout and outFile, the problems are:
1. There are so many overload options for insert operator << , I haven't understood all the options.
2. Then I should replacing cout with sout (STRA sout), however, all my existing code are using cout.
So I prefer some simple configurations rather than modify most of my codes.
There are thousands of cout lines, so there will be mass of work if I have to add a fstream line.
Hope there will be a better solution.
"The insertion operator for ofstream is different from the insertion operator of cout..."
But they do have common streambuf.
If insertion can be broadcasted to multiple streambuf, then my problem can be solved :)
I got a idea about to redefine a class (STRA), and operates on cout and outFile, the problems are:
1. There are so many overload options for insert operator << , I haven't understood all the options.
2. Then I should replacing cout with sout (STRA sout), however, all my existing code are using cout.
IMO It's a good idea you can solve these problems:
1. Use a single template << so it will work with all the existing types supporting cout <<
2. #define cout sout so the preprocessor will modify that for you. ( If you have std::cout you can extend the std namespace to contain sout )