How to override cout in C++?(如何在 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.
这篇关于如何在 C++ 中覆盖 cout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 C++ 中覆盖 cout?
				
        
 
            
        基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
 - 我有静态或动态 boost 库吗? 2021-01-01
 - 如何检查GTK+3.0中的小部件类型? 2022-11-30
 - 这个宏可以转换成函数吗? 2022-01-01
 - 如何通过C程序打开命令提示符Cmd 2022-12-09
 - 在 C++ 中计算滚动/移动平均值 2021-01-01
 - 常量变量在标题中不起作用 2021-01-01
 - 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
 - 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
 - 如何在 C++ 中初始化静态常量成员? 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				