Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 347
Xunil Avatar asked Feb 25 '09 20:02

Xunil


People also ask

Does C++ have a console?

A modern console consists of the keyboard and a window on a computer screen. cin (console in), cout (console out), and cerr (console error) are stream objects that become part of every C++ program. The console objects channel streams of bytes to and from running programs.

What is WriteConsole?

The WriteConsole function writes characters to the console screen buffer at the current cursor position. The cursor position advances as characters are written. The SetConsoleCursorPosition function sets the current cursor position.

What is SetConsoleTextAttribute?

SetConsoleTextAttribute PROC (MS-Windows) Sets the foreground (text) and background color attributes of characters subsequently written to a screen buffer by WriteFile or WriteConsole, or echoed by ReadFile or ReadConsole.


2 Answers

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)

like image 149
MSN Avatar answered Oct 01 '22 06:10

MSN


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

OutputDebugStringW(L"My output string.");
like image 10
Sean Avatar answered Oct 04 '22 06:10

Sean