Direct Output of your C Console Project to a Text File. Either use the code snipplet from this post or create a new C Console Application of your own. Build the Application and make sure there are no compilation or linking errors. Now navigate to the Debug folder of the project as displayed below. Yes this redirection will also work for applications build in Release Mode. Defaultly cout will show on the console screen. We can redirect the output to a file by change the streambuf. But, sometimes we would like to see the message and message and keep them in log file as well. Can we make the cout put on both screen and file using the same line.
17 Jun 2008CPOL
A short article describing how to redirect text output from a console application to a file.
![Output Output](/uploads/1/2/5/8/125870167/668193370.png)
Introduction
I decided to write this article after being unable to find any example code on how to redirect console output to a file. There were examples of writing to a memory stream but I needed to output to a file because I was worried about the potential size of the memory stream required to do what I wanted. I had much help from my friend Armand so I would like to have him credited as a major contributor.
Background
I was experiencing errors when I used the redirection within the arguments sent to the process.
My code was written to back up a MySQL database using the tool mysqldump.exe but it should be easily changed to work with other console applications.
Using the Code
The sample is quite small. I have used comments to describe the process.
History
2008-06-16 Written