routing stdout to window/console?

Started by
5 comments, last by thedustbustr 15 years, 7 months ago
Hi all, I'm sure this is a very simple question if you know the answer but not a particualry easy one to search for either on the web or just by tinkering. How can I route stdout to either the output window of visual studio or simply tell visual studio to give me a console and display it there? I tend to do a lot of debugging with printf's so no longer being able to see stdout other than from a file is irritating. Cheers, Si
Advertisement
Assuming you're asking about adding a console window to a window project, you can use AllocConsole() in your program to give a window application a console window. You'll probably also need to call freopen("CONOUT$", "wb", stdout); or some equivalent in order to redirect stdout to that window.
Cheers, thats what I needed.

Just out of interest, can anyone tell me how to route streams to the visual studio output window?

Thanks,
Si
use
sprintf(str, "...", var1,var2,..)
OutputDebugString (str)

OutputDebugString is a Win32 Api function.
cheers.
I've developed the following code to aid in debugging non-console applications:
#include <windows.h>#include <stdio.h>#include <fcntl.h>#include <io.h>#include <iostream>#include <fstream>using namespace std;void InitializeDebugConsole(){	    //Create a console for this application    AllocConsole();    //Redirect unbuffered STDOUT to the console	HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);    int SystemOutput = _open_osfhandle(intptr_t(ConsoleOutput), _O_TEXT);    FILE *COutputHandle = _fdopen(SystemOutput, "w" );    *stdout = *COutputHandle;    setvbuf(stdout, NULL, _IONBF, 0);    //Redirect unbuffered STDERR to the console	HANDLE ConsoleError = GetStdHandle(STD_ERROR_HANDLE);    int SystemError = _open_osfhandle(intptr_t(ConsoleError), _O_TEXT);    FILE *CErrorHandle = _fdopen(SystemError, "w" );    *stderr = *CErrorHandle;    setvbuf(stderr, NULL, _IONBF, 0);    //Redirect unbuffered STDIN to the console	HANDLE ConsoleInput = GetStdHandle(STD_INPUT_HANDLE);    int SystemInput = _open_osfhandle(intptr_t(ConsoleInput), _O_TEXT);    FILE *CInputHandle = _fdopen(SystemInput, "r" );    *stdin = *CInputHandle;    setvbuf(stdin, NULL, _IONBF, 0);        //make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well    ios::sync_with_stdio(true);}void ShutdownDebugConsole(void){	//Write "Press any key to exit"	HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);	DWORD CharsWritten;	WriteConsole(ConsoleOutput, "\nPress any key to exit", 22, &CharsWritten, 0);	//Disable line-based input mode so we can get a single character	HANDLE ConsoleInput = GetStdHandle(STD_INPUT_HANDLE);	SetConsoleMode(ConsoleInput, 0);	//Read a single character	TCHAR InputBuffer;	DWORD CharsRead;	ReadConsole(ConsoleInput, &InputBuffer, 1, &CharsRead, 0);}

Note that 'ShutdownDebugConsole' does not actually undo the changes to the streams/etc - it's really intended to be called at the end of the application to allow the console contents to be read before it's closed.
Someday I'm going to improve upon it and redirect all the streams to pipes that are read by a background thread that duplicates the pipe's contents to both a file and a console, but for now the console itself works rather well =-)
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
trhis thread on loggers might be of interest, there is some discussion of replacing streambuf's (i.e. redirecting std::cout to somewhere else)

This topic is closed to new replies.

Advertisement