How to output to the console in C++/Windows


Question

When using iostream in C++ on Linux, it displays the program output in the terminal, but in Windows, it just saves the output to a stdout.txt file. How can I, in Windows, make the output appear in the console?

1
15
7/14/2015 11:01:53 PM

Accepted Answer

Since you mentioned stdout.txt I google'd it to see what exactly would create a stdout.txt; normally, even with a Windows app, console output goes to the allocated console, or nowhere if one is not allocated.

So, assuming you are using SDL (which is the only thing that brought up stdout.txt), you should follow the advice here. Either freopen stdout and stderr with "CON", or do the other linker/compile workarounds there.

In case the link gets broken again, here is exactly what was referenced from libSDL:

How do I avoid creating stdout.txt and stderr.txt?

"I believe inside the Visual C++ project that comes with SDL there is a SDL_nostdio target > you can build which does what you want(TM)."

"If you define "NO_STDIO_REDIRECT" and recompile SDL, I think it will fix the problem." > > (Answer courtesy of Bill Kendrick)

16
8/21/2013 9:04:11 PM

You can add a console to a Windows non-console application using the process described in Adding Console I/O to a Win32 GUI App.

There is a whole thread on gamedev.net on the topic.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon