<small id='rRpng'></small><noframes id='rRpng'>

        <bdo id='rRpng'></bdo><ul id='rRpng'></ul>
      <legend id='rRpng'><style id='rRpng'><dir id='rRpng'><q id='rRpng'></q></dir></style></legend>
      <i id='rRpng'><tr id='rRpng'><dt id='rRpng'><q id='rRpng'><span id='rRpng'><b id='rRpng'><form id='rRpng'><ins id='rRpng'></ins><ul id='rRpng'></ul><sub id='rRpng'></sub></form><legend id='rRpng'></legend><bdo id='rRpng'><pre id='rRpng'><center id='rRpng'></center></pre></bdo></b><th id='rRpng'></th></span></q></dt></tr></i><div id='rRpng'><tfoot id='rRpng'></tfoot><dl id='rRpng'><fieldset id='rRpng'></fieldset></dl></div>
    1. <tfoot id='rRpng'></tfoot>

        如何在 C++ 中覆盖 cout?

        时间:2023-05-23

      1. <tfoot id='QJMvm'></tfoot>

                <small id='QJMvm'></small><noframes id='QJMvm'>

              1. <legend id='QJMvm'><style id='QJMvm'><dir id='QJMvm'><q id='QJMvm'></q></dir></style></legend>
                <i id='QJMvm'><tr id='QJMvm'><dt id='QJMvm'><q id='QJMvm'><span id='QJMvm'><b id='QJMvm'><form id='QJMvm'><ins id='QJMvm'></ins><ul id='QJMvm'></ul><sub id='QJMvm'></sub></form><legend id='QJMvm'></legend><bdo id='QJMvm'><pre id='QJMvm'><center id='QJMvm'></center></pre></bdo></b><th id='QJMvm'></th></span></q></dt></tr></i><div id='QJMvm'><tfoot id='QJMvm'></tfoot><dl id='QJMvm'><fieldset id='QJMvm'></fieldset></dl></div>

                  <tbody id='QJMvm'></tbody>

                  <bdo id='QJMvm'></bdo><ul id='QJMvm'></ul>
                • 本文介绍了如何在 C++ 中覆盖 cout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个需求,我需要使用printfcout 将数据显示到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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Visual Studio 2010 中的 C++ 访问修饰符自动缩进慢慢 下一篇:精确的时间测量

                  相关文章

                  最新文章

                • <legend id='O2WYg'><style id='O2WYg'><dir id='O2WYg'><q id='O2WYg'></q></dir></style></legend>

                • <i id='O2WYg'><tr id='O2WYg'><dt id='O2WYg'><q id='O2WYg'><span id='O2WYg'><b id='O2WYg'><form id='O2WYg'><ins id='O2WYg'></ins><ul id='O2WYg'></ul><sub id='O2WYg'></sub></form><legend id='O2WYg'></legend><bdo id='O2WYg'><pre id='O2WYg'><center id='O2WYg'></center></pre></bdo></b><th id='O2WYg'></th></span></q></dt></tr></i><div id='O2WYg'><tfoot id='O2WYg'></tfoot><dl id='O2WYg'><fieldset id='O2WYg'></fieldset></dl></div>
                    <bdo id='O2WYg'></bdo><ul id='O2WYg'></ul>
                • <small id='O2WYg'></small><noframes id='O2WYg'>

                    1. <tfoot id='O2WYg'></tfoot>