久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <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 class="qwawimqqmiuu" id='QJMvm'><tfoot id='QJMvm'></tfoot><dl id='QJMvm'><fieldset id='QJMvm'></fieldset></dl></div>

          <bdo id='QJMvm'></bdo><ul id='QJMvm'></ul>
      2. 如何在 C++ 中覆蓋 cout?

        How to override cout in C++?(如何在 C++ 中覆蓋 cout?)

          <tbody id='rRpng'></tbody>

            <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 class="qwawimqqmiuu" id='rRpng'><tfoot id='rRpng'></tfoot><dl id='rRpng'><fieldset id='rRpng'></fieldset></dl></div>
              • <tfoot id='rRpng'></tfoot>

                  本文介紹了如何在 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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Why do two functions have the same address?(為什么兩個函數的地址相同?)
                  Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復制構造的?)
                  mixing templates with polymorphism(混合模板與多態性)
                  When should I use the keyword quot;typenamequot; when using templates(我什么時候應該使用關鍵字“typename?使用模板時)
                  Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標準庫)
                  gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數模板,而 clang 不能)
                  • <legend id='O2WYg'><style id='O2WYg'><dir id='O2WYg'><q id='O2WYg'></q></dir></style></legend>

                      <tbody id='O2WYg'></tbody>

                  • <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 class="qwawimqqmiuu" 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>

                          • 主站蜘蛛池模板: 欧美黄视频 | 国产suv精品一区 | 午夜精品国产精品大乳美女 | 国产一区二区在线看 | 国产午夜精品一区二区三区视频 | 一区免费视频 | 亚洲精品一区二区在线观看 | 日韩av手机在线 | 国产伦精品一区二区免费 | 天堂免费av | 亚洲精品国产精品国自产观看浪潮 | 精品免费国产 | 成人一区二区视频 | 国产一区不卡 | 亚洲最大黄色 | 亚洲一区久久 | 日韩精品视频网站 | 午夜久久久久久 | 国产精品免费人成网站酒店 | 99视频网站| av一区二区在线观看 | 激情做爰呻吟视频舌吻 | 日韩在线免费视频 | 午夜视频免费在线观看 | 国产精品久久久久久99 | www一区| 黄色网在线 | 91片黄在线观看动漫 | 欧美激情一区二区三区 | 狠狠操av | 国产日本在线观看 | 欧美精品二区 | 日韩视频一区二区 | 久久av片| 久久亚洲欧美 | 黄色特级毛片 | 久久精品久久久久 | 一级片免费播放 | 美女免费视频网站 | 日本福利视频 | 国产伦精品一区二区三区视频网站 |