Solved

C++ Output to Screen and File

Posted on 2014-04-15
13
264 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now