【问题标题】:C++ Writing to file and Console Output at the same time with simple codeC++ 用简单的代码同时写入文件和控制台输出
【发布时间】:2018-04-20 09:11:31
【问题描述】:

我正在尝试将以下整个函数写入文本文件,同时仍保持其控制台输出功能而没有代码冗余。有没有一种简单的方法可以将整个方法的结果同时发布到文件和控制台?

#include<iostream>
#include<fstream>
  void sports(){
      cout<<"\nGame_Code\t\tGane\t\tCost\t\tStart Time\n";
      cout<<"\nSP-9651\t\t Game 1 \t\t60\t\t08:00";
      cout<<"\nSP-9652\t\t Game 2 \t\t60\t\t09:15";
      cout<<"\nSP-9653\t\t Game 3 \t\t55\t\t09:55";
      cout<<"\nSP-9654\t\t Game 4 \t\t55\t\t11:00";
      cout<<"\nSP-9655\t\t Game 5 \t\t50\t\t13:00";
      cout<<"\nSP-9657\t\t Game 7 \t\t45\t\t19:45";
      cout<<"\nSP-9659\t\t Game 8 \t\t70\t\t22:45";
      cout<<"\n\n";
     } 
    int main(){
    //This is for console output
    sports();
    }

【问题讨论】:

标签: c++ fstream ofstream


【解决方案1】:

流可以传递给函数。所以有一个打印功能可以同时输出。

void print(std::ostream &os1, std::ostream &os2, const std::string &str)
{
    os1 << str;
    o22 << str;
}

void sports()
{
    std::fstream file("filename");

    print(std::cout, file, "\nSP-9651\t\t Game 1 \t\t60\t\t08:00");
    print(std::cout, file, "\nSP-9652\t\t Game 2 \t\t60\t\t09:00");
    print(std::cout, file, "\nSP-9653\t\t Game 3 \t\t60\t\t10:00");
    //... etc
}

int main()
{
    sports();
    return 0;
}

【讨论】:

    【解决方案2】:

    是的,您可以从函数中返回std::string 并将其保存到变量中。 然后您可以使用该变量在控制台或/和文件上打印它。

    std::string sports(){
      std::stringstream ss;
      ss<<"\nGame_Code\t\tGane\t\tCost\t\tStart Time\n";
      // ...
      return ss.str();
     }
    

    函数应该只有一个目的。在您的情况下,该目的是创建字符串。单一责任原则。

    【讨论】:

      【解决方案3】:
         #include<iostream>
         #include<fstream>
         using namespace std;
        void sports(){
            ofstream fs("abc");
            cout<<"\nGame_Code\t\tGane\t\tCost\t\tStart Time\n";
            fs<<"\nGame_Code\t\tGane\t\tCost\t\tStart Time\n";
      
           } 
          int main(){
          //This is for console output
          sports();
          }
      

      【讨论】:

        【解决方案4】:

        你可以编写简单的函数来俱乐部它??

        #include<iostream>
        #include<fstream>
        using namespace std;
        
          void myoutput( fstream &fs, string str)
          {
              cout << str;
              fs << str;
        
          }
          void sports(){
              fstream fs("abc");
              myoutput(fs,"\nGame_Code\t\tGane\t\tCost\t\tStart Time\n ..");
        
             } 
            int main(){
            //This is for console output
            sports();
            }
        

        【讨论】:

          【解决方案5】:

          我建议使用一个将 - 写入文件和 cout 的函数。我的示例函数只接受文本。所有给定的文本都附加到文件中:

          void dual_write(const std::string& text) {
              // Print to console
              std::cout << text;
          
              // Print to file (append)
              std::ofstream file("out.txt", std::ios_base::app);
              file << text;
          }
          

          通过调用它 dual_write("\nGame_Code\t\tGane\t\tCost\t\tStart Time\n"); 等等。

          【讨论】:

            猜你喜欢
            • 2020-04-10
            • 2017-09-30
            • 1970-01-01
            • 1970-01-01
            • 2012-12-18
            • 1970-01-01
            • 2013-08-29
            相关资源
            最近更新 更多