如何覆盖cout在C ++? [英] How to override cout in C++?
问题描述
我有一个要求,我需要使用 printf
和 cout
显示数据到 console和file
。
对于 printf
我已经做到了 cout
我很努力,怎么办? p>
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!\n";
int howbad = 10;
printf(outfile, "\n--------\n");
//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
(caps,上面代码的注释部分)。但我想使用正常 std :: cout
,因此如何覆盖它。它应该适用于 sting和变量
如
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 << "\n" << 2 << "\n") )
{
std::cerr << "all went fine\n";
}
else
{
std::cerr << "bad bad stream\n";
}
}
这提供了一个干净的界面,控制台和文件。
您可能需要添加flush方法或以追加方式打开文件。
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.
这篇关于如何覆盖cout在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!