20

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Xunil
  • 295
  • 2
  • 4
  • 11

13 Answers13

17

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)

larrylampco
  • 597
  • 1
  • 9
  • 33
MSN
  • 53,214
  • 7
  • 75
  • 105
12

For debugging in Visual Studio you can print to the debug console:

OutputDebugStringW(L"My output string.");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sean
  • 2,033
  • 2
  • 23
  • 28
6

If you're using Visual Studio you need to modify the project property: Configuration Properties -> Linker -> System -> SubSystem.

This should be set to: Console (/SUBSYSTEM:CONSOLE)

Also you should change your WinMain to be this signature:

int main(int argc, char **argv)
{
    //...
    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
6

If you have a none-console Windows application, you can create a console with the AllocConsole function. Once created, you can write to it using the normal std::cout methods.

  • I think you have to still redirect the io descriptors to get it to work (like the guicon example does). Or alternatively just use OutputDebugString instead. – rogerdpack Mar 20 '11 at 04:05
4

Whether to use subsystem:console or subsystem:windows kind of depends on whether how you want to start your application:

  • If you use subsystem:console, then you get all of the stdout written to the terminal. The trouble is that if you start the application from the Start Menu/Desktop, you (by default) get a console appearing as well as the application window (which can look pretty ugly).
  • If you use subsystem:windows, you won't get stdout/stderr even if you run the application from a DOS window, Cygwin, or other terminal.

If you want the middle way which is to output to the terminal IF the application was started in a terminal, then follow the link that Luke provided in his solution (http://dslweb.nwnexus.com/~ast/dload/guicon.htm)

For reference, I ran into this problem with an application that I want to run in either normal Windows mode or batch mode (that is, as part of a script) depending on command-line switches. The whole differentiation between console and Windows applications is a bit bizarre to Unix folks!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AJ.
  • 41
  • 2
  • 2
    You need to change the example in the link a bit: replace AllocConsole() with AttachConsole(ATTACH_PARENT_PROCESS) to output to the same console that started the application, if the application was started on a console. If not this function will return 0. – Ruud Mar 11 '10 at 19:02
  • Could you reefer me to the MSDN documentation where defined "If you use subsystem:windows, you won't get stdout/stderr" – N0dGrand87 May 16 '18 at 22:15
  • You can also pipe to cat. `myapp.exe | cat` then the stdout will work in a console even with subsystem:windows – v.oddou May 09 '23 at 10:31
4

The AllocConsole Windows API function will create a console window for your application.

jeffm
  • 3,120
  • 1
  • 34
  • 57
3

First off, what compiler or dev environment are you using? If Visual Studio, you need to make a console application project to get console output.

Second,

std::cout << "Hello World" << std::endl;

should work in any C++ console application.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
  • 8
    No, that sends to standard output - and that does not always write to the console (depending on how it is allocated). Hence the OP's question, and the reason for me to read this thread nearly two years later! :-) – winwaed Jan 13 '11 at 19:46
3

If you're using Visual Studio, it should work just fine!

Here's a code example:

#include <iostream>

using namespace std;

int main (int) {
    cout << "This will print to the console!" << endl;
}

Make sure you chose a Win32 console application when creating a new project. Still you can redirect the output of your project to a file by using the console switch (>>). This will actually redirect the console pipe away from the stdout to your file. (for example, myprog.exe >> myfile.txt).

I wish I'm not mistaken!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmed
  • 11,063
  • 16
  • 55
  • 67
2

There is a good solution

if (AllocConsole() == 0)
{
    // Handle error here. Use ::GetLastError() to get the error.
}

// Redirect CRT standard input, output and error handles to the console window.
FILE * pNewStdout = nullptr;
FILE * pNewStderr = nullptr;
FILE * pNewStdin = nullptr;

::freopen_s(&pNewStdout, "CONOUT$", "w", stdout);
::freopen_s(&pNewStderr, "CONOUT$", "w", stderr);
::freopen_s(&pNewStdin, "CONIN$", "r", stdin);

// Clear the error state for all of the C++ standard streams. Attempting to accessing the streams before they refer
// to a valid target causes the stream to enter an error state. Clearing the error state will fix this problem,
// which seems to occur in newer version of Visual Studio even when the console has not been read from or written
// to yet.
std::cout.clear();
std::cerr.clear();
std::cin.clear();

std::wcout.clear();
std::wcerr.clear();
std::wcin.clear();
YuryChu
  • 181
  • 1
  • 3
  • 13
2

Your application must be compiled as a Windows console application.

spoulson
  • 21,335
  • 15
  • 77
  • 102
1

If using MinGW, add an option, -Wl,subsystem,console or -mconsole.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jarekczek
  • 7,456
  • 3
  • 46
  • 66
1

I assume you're using some version of Visual Studio? In windows, std::cout << "something"; should write something to a console window IF your program is setup in the project settings as a console program.

Rob K
  • 8,757
  • 2
  • 32
  • 36
0

You don't necessarily need to make any changes to your code (nor to change the SUBSYSTEM type). If you wish, you also could simply pipe stdout and stderr to a console application (a Windows version of cat works well).

Community
  • 1
  • 1
jamesdlin
  • 81,374
  • 13
  • 159
  • 204