Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

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
0
college_student
Asked:
college_student
  • 6
  • 6
1 Solution
 
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
 
käµfm³d 👽Commented:
OK. Can you show an example of each?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
college_studentAuthor 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:
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now