| fengxu (13) | |
|
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 Thanks. | |
|
|
|
| olredixsis (194) | |||
|
Yes! some long codes can be reduced in one line only (lol). same line but too long. going back to your question. Why would you want to make it in one line only?
EDIT: make code more efficient, cleaner and easier to understand. | |||
|
Last edited on
|
|||
| fengxu (13) | |||||
|
Sorry that I didn't say my question clearly. I want to put val 'm' to both screen and file. I can do it by:
Can I do some streambuf configures, then:
| |||||
|
Last edited on
|
|||||
| olredixsis (194) | |
|
Hmm... did you try operator overloading?? overload the cout?... | |
|
|
|
| fengxu (13) | |
|
Thanks for your reply. 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. Thx. | |
|
|
|
| olredixsis (194) | |
|
Hmm... nevermind using operator<< I'm not an expert, so I think your problem is a challenge for experts.. Do you really need to make it in a single line? the insertion operator for ofstream is different from the insertion operator of cout... Let's hope an expert would post in your thread and solve your problem.. Goodluck | |
|
|
|
| fengxu (13) | |
|
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 :) Thanks for your warm reply. | |
|
Last edited on
|
|
| Bazzy (6281) | ||
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 )
| ||
|
|
||
| fengxu (13) | ||
Thanks. | ||
|
Last edited on
|
||
| fengxu (13) | |||
|
I got some material from http://www.codeproject.com/KB/cpp/functemplates.aspx It almost fixed my problem, except 'endl'.
| |||
|
Last edited on
|
|||
| olredixsis (194) | |||||
|
hmm... Good for you.. try using
or maybe
EDIT: endl is a manipulator, I think you must revised your template declaration and include endl there.
| |||||
|
Last edited on
|
|||||
| fengxu (13) | |||
sout << "\n" works :)And for "endl", which is a function template, the following code should be added.
So, all fine now. Thank you guys. | |||
|
|
|||