First of all, why do we need to do this? Generally, our purpose is to facilitate debugging.
Windows provides you with a series of APIs to complete this function, such as readconsole and writeconsole. For details, see msdn.
1. Call the allocconsole function to create a console for your process. This API is used to attache the current process to a newly created console. You can also set the console title by calling setconsoletitle (tstrname.
2. Use writeconsoleoutput to output information to the console. Before the output, you must first obtain the console handle, which is obtained through getstdhandle (std_output_handle) and then the information group is woven into the console format, then output.
3. Close the console. If this console is not required, call freeconsole to detach the current process from the console.
4. You can create a thread to monitor the input and output of your console. You can create a thread to obtain the handle of the standard input and output console in the thread, and then monitor its events cyclically, then process different events.
The code example of the above solution is as follows:
Http://blog.csdn.net/windcsn/archive/2005/11/27/537737.aspx
Solution 2:
In project | setting..., create a command in the Post-builder step option, and enter:
Editbin/subsystem: Console $ (outdir)/filename.exe
(Filename is an executable file name)
You can use printf or STD: cout to output data in the console. For example, if your executable file name is helloworld.exe, the new command will be:
Editbin/subsystem: Console $ (outdir)/helloworld.exe
The third solution is to use trace, compile the debug project, and run your executable file directly. You can use dbgview to obtain the output.
The following is a simple code:
// Windows2console. h <br/> # ifndef _ window2console_h _ <br/> # DEFINE _ window2console_h _ </P> <p> // create a command line output window <br/> class windows2console <br />{< br/> public: <br/> // constructor <br/> windows2console (void); </P> <p> // destructor <br/> ~ Windows2console (void); </P> <p> PRIVATE: <br/> // create <br/> void setup (void); <br/> }; </P> <p> # endif
// Windows2console. CPP <br/> # include "stdafx. H "<br/> # include" windows2console. H "</P> <p> # include <Io. h> <br/> # include <fcntl. h> <br/> # include <iostream> <br/> using namespace STD; </P> <p> /******************************** **************************************** * *********** <br/> * function name windows2console <br/> * function description constructor, create Console window <br/> * function parameter void <br/> * No return value <br/> * <br/> ************ ******************** **************************************** * **********/<Br/> windows2console:: windows2console (void) <br/>{< br/> allocconsole (); <br/> setup (); <br/>}</P> <p> /*************************** **************************************** * **************** <br/> * function name ~ Windows2console <br/> * describes destructor, close the console window <br/> * function parameter void <br/> * No return value <br/> * <br/> ************ **************************************** * *****************************/<br/> windows2console: :~ Windows2console (void) <br/>{< br/> freeconsole (); <br/>}</P> <p> /*************************** **************************************** * **************** <br/> * function name setup <br/> * function Creation, set the console window to read and write <br/> * function parameter void <br/> * return value void <br/> * <br/> ********** **************************************** * *******************************/<br/> void windows2console:: setup (void) <br/>{< br/> * stdin = * (_ fdopen (_ open_osfhandle (intptr_t): getstdhandle (std_input_handle), _ o_text ), "R"); <br/> * stdout = * (_ fdopen (_ open_osfhandle (intptr_t): getstdhandle (std_output_handle), _ o_text ), "WT"); <br/> ios_base: sync_with_stdio (); <br/>}