Write file and see output on console

So i am trying to solve the bunny exercise that was posted in this forum a while ago.
I already have the whole program and now i was trying to write all output on a txt, but my problem is that if i write everything on the txt, the console does not show anything. Is there any way to write the .txt and to see the output on the console?
As you write to the file, you could mirror the statement for a cout.
i.e.
1
2
outFile << variable;
std::cout << variable;


Write everything to a buffer before writing to the file. Then write the buffer to a file, then output the buffer to the console.

Or once you have finished, just read the file in to a buffer, then output the buffer?
I was wondering since i already have the program all done with some functions returning "cout", if there was any way i could do that without having to rewrite everything?
An overloaded function, returning an ostream, or...?
nope, just a regular function like:

1
2
3
4
5
6
7
8
9
10
void readlist(bunnylist list){
     bunny aux; aux=list->first;
     cout << "There are " << list->number << " bunnies.\n";
     while(aux!= NULL){
     cout << colors_l[aux-> color] << " " << sexes_l[aux->sex] << " " << names_l[aux->name] << " is " << aux->age << " years old";
     if (aux->rad == 1) cout << " and is infected";
     cout << ".\n";
     aux = aux->seg;}
     cout << "\n";
     } 


but i have more function that print on the screen, so my question is that i have to change each funtion or can i write something in the main part to print in both the txt and the console simultaneously
hmm this is where planning would have been great! :P

Either create a function that opens a file and appends to the end of the file. Creating a file which is mirroring the couts. And call this file with the same output each time( save the cout's first, cout, then call the function ).

Create a global string, and each time you do a cout, += to the string, with what was cout'ed. Write the string to the file at the end off the program.

Or a re-write! Unless someone else in the forum has a better idea! (:
Guess i will have to do either one of the first two.
Any help with the first one? Guess i can just change the couts to do that or something similar. Thanks for the help
It is just weird that if i am programming something and i'd like to store date, i'd like to see it both on the console and on the txt file, so this shouldn't be that rare.
So i wrote this

1
2
3
4
5
6
7
void reads(string st){
     ofstream myfile;
     myfile.open ("bunny.txt", ios::app);
     myfile << st;
     myfile.close();
     cout << st;
     }


is it very demaning to keep opening and closing the txt?
It shouldn't be too demanding as it's only a small program.

And that function looks fine. The only thing with this, is that you have ios::app. Each run of the program, it will get bigger and bigger( not by much, but... ). If you don't want to have each run saved in the file, you may want to open the file at the start of the program, just to remove the contents. i.e. Just open and write.
But since everytime i do cout i open and write, i'd just have the last thing cout'ed printed. I just have to make sure if i run again the program i erase the previous txt.
I think it is too complex to me, but i want to see others answer
ios::app will append to the end of the file. So everything you cout & reads( string ) will be in the file.
Topic archived. No new replies allowed.