Jump to content

  • Log In with Google      Sign In   
  • Create Account


routing stdout to window/console?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Si Tovey   Members   -  Reputation: 145

Like
0Likes
Like

Posted 25 September 2008 - 03:01 AM

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

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9423

Like
0Likes
Like

Posted 25 September 2008 - 03:07 AM

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.

#3 Si Tovey   Members   -  Reputation: 145

Like
0Likes
Like

Posted 25 September 2008 - 03:31 AM

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

#4 Andrew Kabakwu   Members   -  Reputation: 662

Like
0Likes
Like

Posted 25 September 2008 - 03:40 AM

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

OutputDebugString is a Win32 Api function.

#5 Si Tovey   Members   -  Reputation: 145

Like
0Likes
Like

Posted 25 September 2008 - 03:44 AM

cheers.

#6 Extrarius   Members   -  Reputation: 1412

Like
0Likes
Like

Posted 25 September 2008 - 03:46 AM

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

#7 thedustbustr   Members   -  Reputation: 191

Like
0Likes
Like

Posted 25 September 2008 - 03:05 PM

trhis thread on loggers might be of interest, there is some discussion of replacing streambuf's (i.e. redirecting std::cout to somewhere else)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS