Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How can i print everything which is printed on the console window into a file?
Posted

Just redirect the standard output. For instance, if your program's name is myapp.exe then, in the console window, type:
myapp.exe > out.txt

that would generate the text file out.txt containing all the standard output of your application.

You may also programmatically redirect the standard out, see: "How to redirect stdout to a file from a Visual C/C++ program" at MSDN[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Jul-14 4:38am    
5ed. However, sometimes we need to see the console and have the file at the same execution. When a console output is redirected, the text is not show in the console, so we need more universal solution. I explained the way to do it in Solution 2, please see.
—SA
As the alternative to Solution 1:

Sometimes we need to see the console and have the file at the same execution. When a console output is redirected, the text is not show in the console. Here is the solution:

Abstract out console operations like Console.Write and Console.WriteLine: create your own methods, perhaps reproduce the same interface as Console's (it will help you to refactor existing applications easily). In the implementation of your own methods, use existing console method plus methods writing to a file. Depending on some options, switch output to console, file, or both. The user can choose appropriate options (including also the file name) in a command line.

—SA
 
Share this answer
 
Comments
CPallini 29-Jul-14 4:53am    
5.
Sergey Alexandrovich Kryukov 29-Jul-14 10:51am    
Thank you, Carlo.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900