输出到2个流 [英] output to 2 streams
问题描述
我的程序输出到cout,我也希望它输出到LOG文件。
有一个简单的方法吗?
弗雷泽。
My program outputs to cout and I want it to output to a LOG file as well.
Is there a simple way to do that?
Fraser.
推荐答案
" Fraser Ross" < fraserATmembers.v21.co.unitedkingdom>在消息中写道
"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
我的程序输出到cout,我也希望它输出到LOG文件。
有一个简单的方法吗?
My program outputs to cout and I want it to output to a LOG file as well.
Is there a simple way to do that?
这个简单的方法怎么样 -
std :: ofstream out1(" xxx.log");
.. ..
std :: cout<< 记录和打印的东西;
out1<< 记录和打印的东西;
您也可以使用Macro技巧来完成这项工作,例如 -
#define PRINT (xxx)std :: cout<< 记录和打印的东西; \
out1<< 记录和打印的东西;
int main()
{
//...
PRINT(要打印的东西);
}
-Sharad
How about this simple way -
std::ofstream out1("xxx.log");
....
std::cout << "Something to log and print";
out1 << "Something to log and print";
You could also use Macro trick to get this done, something like -
#define PRINT(xxx) std::cout << "Something to log and print"; \
out1 << "Something to log and print";
int main()
{
//...
PRINT ("Something to print");
}
-Sharad
这样做了,比如 -
this done, something like -
#define PRINT(xxx)std :: cout<< 记录和打印的东西; \
out1<< 记录和打印的东西;
#define PRINT(xxx) std::cout << "Something to log and print"; \
out1 << "Something to log and print";
应该是 -
#define PRINT(xxx)std :: cout<< XXX; \
out1<< xxx;
Should be --
#define PRINT(xxx) std::cout << xxx; \
out1 << xxx;
可以从
ostringstream构造ostream_iterator或ostreambuf_iterator吗?例如:
std :: ostringstream oStr;
std :: ostream_iterator it(oStr);
basic_ostringstream继承自basic_ostream。 ostream_iterator有一个带有basic_ostream的
构造函数。
Fraser。
Can a ostream_iterator or ostreambuf_iterator be constructed from a
ostringstream? E.g.:
std::ostringstream oStr;
std::ostream_iterator it(oStr);
basic_ostringstream inherits from basic_ostream. ostream_iterator has a
constructor taking a basic_ostream.
Fraser.
这篇关于输出到2个流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!