Link to home
Start Free TrialLog in
Avatar of college_student
college_student

asked on

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

What do you know about I/O in C++?
Avatar of college_student
college_student

ASKER

I really do not know much, I only know how to send output to a file and screen but not at the same time.
OK. Can you show an example of each?
ASKER CERTIFIED SOLUTION
Avatar of college_student
college_student

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So why can those not be in the same block of code? Would that not achieve writing to both places?
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?
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.
It didn't work
Are you checking for exceptions?
What do you mean?
i got the help i needed
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