如何在 C++ 中覆盖 cout?
我有一个需求,我需要使用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?
#ifdef _MSC_VER
#define GWEN_FNULL "NUL"
#define va_copy(d,s) ((d) = (s))
#else
#define GWEN_FNULL "/dev/null"
#endif
#include <iostream>
#include <fstream>
using namespace std;
void printf (FILE * outfile, const char * format, ...)
{
va_list ap1, ap2;
int i = 5;
va_start(ap1, format);
va_copy(ap2, ap1);
vprintf(format, ap1);
vfprintf(outfile, format, ap2);
va_end(ap2);
va_end(ap1);
}
/* void COUT(const char* fmt, ...)
{
ofstream out("output-file.txt");
std::cout << "Cout to file";
out << "Cout to file";
}*/
int main (int argc, char *argv[]) {
FILE *outfile;
char *mode = "a+";
char outputFilename[] = "PRINT.log";
outfile = fopen(outputFilename, mode);
char bigfoot[] = "Hello
World!
";
int howbad = 10;
printf(outfile, "
--------
");
//myout();
/* then i realized that i can't send the arguments to fn:PRINTs */
printf(outfile, "%s %i",bigfoot, howbad); /* error here! I can't send bigfoot and howbad*/
system("pause");
return 0;
}
我已经在 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
int i = 5;
cout << "Hello world" << i <<endl;
或者无论如何都可以捕获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
#include <fstream>
#include <iostream>
class DualStream
{
std::ofstream file_stream;
bool valid_state;
public:
DualStream(const char* filename) // the ofstream needs a path
:
file_stream(filename), // open the file stream
valid_state(file_stream) // set the state of the DualStream according to the state of the ofstream
{
}
explicit operator bool() const
{
return valid_state;
}
template <typename T>
DualStream& operator<<(T&& t) // provide a generic operator<<
{
if ( !valid_state ) // if it previously was in a bad state, don't try anything
{
return *this;
}
if ( !(std::cout << t) ) // to console!
{
valid_state = false;
return *this;
}
if ( !(file_stream << t) ) // to file!
{
valid_state = false;
return *this;
}
return *this;
}
};
// let's test it:
int main()
{
DualStream ds("testfile");
if ( (ds << 1 << "
" << 2 << "
") )
{
std::cerr << "all went fine
";
}
else
{
std::cerr << "bad bad stream
";
}
}
这提供了一个干净的界面,并为控制台和文件输出相同的内容.您可能希望添加刷新方法或以追加模式打开文件.
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.
相关文章