问题描述
我有一个需求,我需要使用printf
和cout
将数据显示到console 和file
中.对于 printf
我已经做到了,但是对于 cout
我很挣扎,怎么做?
I have a requirement, I need to use printf
and cout
to display the data into console and file
as well.
For printf
I have done it but for cout
I am struggling, how to do it?
我已经在 COUT
(大写,上面代码的注释部分)中完成了.但是我想使用普通的std::cout
,那么我该如何覆盖它.它应该适用于 sting 和 variables
就像
I have done it in COUT
(caps, the commented part for the code above) . But I want to use normal std::cout
, so how can I override it. And it should work for both sting and variables
like
或者无论如何都可以捕获stdout
数据,以便它们也可以轻松写入文件和控制台
.
Or are there anyway to capture stdout
data, so that they can be easily written into file and console
as well.
推荐答案
覆盖 std::cout
的行为是一个非常糟糕的主意,因为其他开发人员将很难理解使用std::cout
表现不正常.
Overriding the behaviour of std::cout
is a really bad idea as other developers will have a hard time understanding that the use of std::cout
doesn't behave as usual.
用一个简单的类明确你的意图
Make your intention clear with a simple class
这提供了一个干净的界面,并为控制台和文件输出相同的内容.您可能希望添加刷新方法或以追加模式打开文件.
This provides a clean interface and outputs the same for both the console and the file. You may want to add a flush method or open the file in append mode.
这篇关于如何在 C++ 中覆盖 cout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!