Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ Output to Screen and File

Posted on 2014-04-15
13
Medium Priority
?
298 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

610 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