Solved

C++ Output to Screen and File

Posted on 2014-04-15
13
270 Views
Last Modified: 2014-04-28
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
Comment
Question by:college_student
  • 6
  • 6
13 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40003210
What do you know about I/O in C++?
0
 

Author Comment

by:college_student
ID: 40003214
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40003216
OK. Can you show an example of each?
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Accepted Solution

by:
college_student earned 0 total points
ID: 40003220
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40003223
So why can those not be in the same block of code? Would that not achieve writing to both places?
0
 

Author Comment

by:college_student
ID: 40003224
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40003226
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
 

Author Comment

by:college_student
ID: 40003231
It didn't work
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40003232
Are you checking for exceptions?
0
 

Author Comment

by:college_student
ID: 40003233
What do you mean?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40003234
0
 

Author Closing Comment

by:college_student
ID: 40025567
i got the help i needed
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 40026708
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question