C++ Output to Screen and File

hello, does anyone know how to send output to a file and screen simultaneously?
Currently my code only sends the output to a txt file and not the screen.

Can anyone help me?

Im not sure if i need to post my code for this, but let me know if you need me to
college_studentAsked:
Who is Participating?
 
college_studentConnect With a Mentor Author Commented:
cout << "Hello World" << endl;

ofstream output_file;
output_file.open("results.txt");
output_file << "Hello World"<< endl;
output_file.close();

Open in new window

0
 
käµfm³d 👽Commented:
What do you know about I/O in C++?
0
 
college_studentAuthor Commented:
I really do not know much, I only know how to send output to a file and screen but not at the same time.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
käµfm³d 👽Commented:
OK. Can you show an example of each?
0
 
käµfm³d 👽Commented:
So why can those not be in the same block of code? Would that not achieve writing to both places?
0
 
college_studentAuthor Commented:
Im not sure why. my professor did not really teach this topic to the class, he only posted notes on how to send output to a file. Should the output_file be its own block of code?
0
 
käµfm³d 👽Commented:
I'm saying:  What if you created a new function that had all of that in it? Then you simply call the function, passing in whatever string you want to output as a parameter. Then you have--barring any exceptions--the output going to two places by way of just one function call by you.
0
 
college_studentAuthor Commented:
It didn't work
0
 
käµfm³d 👽Commented:
Are you checking for exceptions?
0
 
college_studentAuthor Commented:
What do you mean?
0
 
käµfm³d 👽Commented:
0
 
college_studentAuthor Commented:
i got the help i needed
0
 
ZoppoCommented:
Hi college_student,

I didn't write this before since I thought this is a homework question which you had to try to solve yourself. But it seems you now found a solution, so I guess it's ok to show you a maybe interesting solution which is i.e. described in the section Tee Streams at http://wordaligned.org/articles/cpp-streambufs.

There you can find a class which allows outputting data to multiple streams simply somehow like this:
int main()
{
    std::ofstream log("hello-world.log");
    teestream tee(std::cout, log);
    tee << "Hello, world!\n";
    return 0;
}

Open in new window

ZOPPO
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.