views:

3840

answers:

10

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  A: 

Your application must be compiled as a Windows console application.

spoulson
A: 

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
+6  A: 

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
+7  A: 

You can add a console to a windows non-console application using the process described here: http://dslweb.nwnexus.com/~ast/dload/guicon.htm

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

luke
A: 

If you're using visual studio you need to modify the project property: 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; 
}
Brian R. Bondy
A: 

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. (e.g. myprog.exe >> myfile.txt).

i wish i'm not mistaken!

Galilyou
+5  A: 

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.

anon
+4  A: 

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

jeffm
+5  A: 

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.

MSN
+2  A: 

Whether to use subsystem:console or subsystem:windows kinda depends on whether how you want to start your app:

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

If you want the middle way which is to output to the terminal IF the app 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 app that I want to run in either normal windows mode or batch mode (ie in part of a script) depending on command-line switches. The whole differentiation between console and windows apps is a bit bizarre to Unix folks!

AJ
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 v A