Solved

C++ Output to Screen and File

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

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 74

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 74

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 74

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 74

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 74

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 30

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

708 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

20 Experts available now in Live!

Get 1:1 Help Now