Solved

C++ Output to Screen and File

Posted on 2014-04-15
13
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

  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 …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

737 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