4

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();
    }
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 2
    See [Copy, load, redirect and tee using C++ streambufs](http://wordaligned.org/articles/cpp-streambufs). Dealing with c++ streambufs is not simple though. – user7860670 Apr 20 '18 at 09:15
  • @FredrickChege the link in VTT's is definitely what you need. The section you're interested in is __Tee streams__. – Jabberwocky Apr 20 '18 at 09:24
  • 2
    Possible duplicate of [trying to write std:out and file at the same time](https://stackoverflow.com/questions/13665090/trying-to-write-stdout-and-file-at-the-same-time) – 463035818_is_not_an_ai Apr 20 '18 at 09:25

5 Answers5

3

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;
}
acraig5075
  • 10,588
  • 3
  • 31
  • 50
1

Yes, you can return std::string from the function and save it to a variable. 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.

Petar Velev
  • 2,305
  • 12
  • 24
0
   #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();
    }
code707
  • 1,663
  • 1
  • 8
  • 20
0

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();
    }
code707
  • 1,663
  • 1
  • 8
  • 20
0

I suggest using a function, that writes both - to a file and 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"); and so on.

nada
  • 2,109
  • 2
  • 16
  • 23