Direct Output of your C++ Console Project to a Text File

On this website most of the Sample C++ Projects have provided some sort of output in console window. Now this post provides you the information on how you can direct or basically redirect the ouput of your C++ project to a text file. Yes without information on how to create and write data to files, you can simply redirect the output of your C++ project to a text file which you can open in any text editor like Notepad, Wordpad, etc. The given below sample C++ Project simply writes a string to the output console.

Redirection of C++ Project Output

Redirection of C++ Project Output

As you would have noted that the above C++ Code simply writes to the console window as all the projects on this website have been doing beginning with C++ Hello World Project. 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.

Testing Redirection of a C++ Project to a Data File

Testing Redirection of a C++ Project to a Data File

Have a close look at the above screenshot of the console window. The Compiled Application is present in the Debug Folder with the name Redirection.exe as the Project Name entered in the Application Wizard was Redirection. Text note that instead of running the Application , here it is run as

Redirection.exe > a.txt

which effectively creates a.txt file and stores the output of the application.

// Redirection.cpp : Defines the entry point for the console application.// Visit Tapkaa.com for C++ Samples and Tutorials#include "stdafx.h"#include <iostream>int _tmain(int argc, _TCHAR* argv[]){std::cout << "String Output of Sample C++ Projectn";return 0;}

In this C++ Tutorial, you have learnt how you can re-direct the output of a console application to any text file. When the output of the application is redirected, the application does not prints to the console window as is evident from the test above. Yes you can even redirect the output from dos commands as well which does not require to provide any user input like the directory command or dir in short.