tags:

views:

746

answers:

3

hello, what C++ functions should i use to output text to the "Output" window in Visual Studio?

i tried printf() but it doesnt show up.

thanks!

+4  A: 

OutputDebugString function will do it.

example code

    void CClass::Output(const char* szFormat, ...)
{
    char szBuff[1024];
    va_list arg;
    va_start(arg, szFormat);
    _vsnprintf(szBuff, sizeof(szBuff), szFormat, arg);
    va_end(arg);

    OutputDebugString(szBuff);
}
Sorantis
That fixed-length buffer is a buffer-overflow waiting to happen.
avakar
That's why he's using vsnprintf
anon
Right, my bad. [15char]
avakar
There's still a problem with this. _vsnprintf may truncate the formatted string to fit the buffer, but if this happens the string won't be nul-terminated. See http://msdn.microsoft.com/en-us/library/1kt27hek.aspx and http://stackoverflow.com/questions/357068.
ChrisN
+4  A: 

If this is for debug output then OutputDebugString is what you want. A useful macro :

#define DBOUT( s )            \
{                             \
   std::ostringstream os_;    \
   os << s;                   \
   OutputDebugString( os_.str().c_str() );  \
}

This allows you to say things like:

DBOUT( "The value of x is " << x );

You can extend this using the __LINE__ and __FILE__ macros to give even more information.

anon
+6  A: 

Use the OutputDebugString function or the TRACE macro (MFC) which lets you do printf-style formatting:

int x = 1;
int y = 16;
float z = 32.0;
TRACE( "This is a TRACE statement\n" );    
TRACE( "The value of x is %d\n", x );
TRACE( "x = %d and y = %d\n", x, y );
TRACE( "x = %d and y = %x and z = %f\n", x, y, z );
Pukku