简体   繁体   English

C ++使用简单的代码同时写入文件和控制台输出

[英]C++ Writing to file and Console Output at the same time with simple code

I am trying to write the following entire function to a text file while still maintaining its console output functionality without having code redundancy. 我试图将以下整个功能写入文本文件,同时仍保持其控制台输出功能而没有代码冗余。 Is there a simple way to post an entire method's result to a file and console at the same time? 是否有一种简单的方法可以将整个方法的结果同时发布到文件和控制台?

#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();
    }

Streams can be passed to functions. 流可以传递给函数。 So have a print function that does both outputs. 因此,有一个可以同时输出两个输出的打印功能。

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;
}

Yes, you can return std::string from the function and save it to a variable. 是的,您可以从函数返回std::string并将其保存到变量中。 Then you can use that variable to print it on the console or/and on a file. 然后,您可以使用该变量在控制台或/和文件上打印该变量。

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

The function should have only one purpose. 该功能应仅具有一个目的。 In your case, that purpose is to create the string.Single responsibility principle. 在您的情况下,该目的是创建字符串。单一责任原则。

   #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();
    }

You can write simple function to club it ?? 您可以编写简单的函数来汇总它?

#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();
    }

I suggest using a function, that writes both - to a file and cout. 我建议使用同时写入文件和cout的函数。 My example function just takes text. 我的示例函数只接受文本。 All given text is appended to the file: 所有给定的文本都会附加到文件中:

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;
}

Call it via dual_write("\\nGame_Code\\t\\tGane\\t\\tCost\\t\\tStart Time\\n"); 通过dual_write("\\nGame_Code\\t\\tGane\\t\\tCost\\t\\tStart Time\\n");调用它dual_write("\\nGame_Code\\t\\tGane\\t\\tCost\\t\\tStart Time\\n"); and so on. 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM