Sign in to follow this  
Si Tovey

routing stdout to window/console?

Recommended Posts

Si Tovey    145
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

Share this post

Link to post
Share on other sites
SiCrane    11839
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.

Share this post

Link to post
Share on other sites
Extrarius    1412
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
//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

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 =-)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this