How to write to the Output window in Visual Studio?

C++Visual C++

C++ Problem Overview


Which function should I use to output text to the "Output" window in Visual Studio?

I tried printf() but it doesn't show up.

C++ Solutions


Solution 1 - C++

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);
}

Solution 2 - C++

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.

For those in Windows and wide character land:

#include <Windows.h>
#include <iostream>
#include <sstream>

 #define DBOUT( s )            \
{                             \
   std::wostringstream os_;    \
   os_ << s;                   \
   OutputDebugStringW( os_.str().c_str() );  \
}

Solution 3 - C++

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

Solution 4 - C++

Useful tip - if you use __FILE__ and __LINE__ then format your debug as:

"file(line): Your output here"

then when you click on that line in the output window Visual Studio will jump directly to that line of code. An example:

#include <Windows.h>
#include <iostream>
#include <sstream>

void DBOut(const char *file, const int line, const WCHAR *s)
{
    std::wostringstream os_;
    os_ << file << "(" << line << "): ";
    os_ << s;
    OutputDebugStringW(os_.str().c_str());
}

#define DBOUT(s)       DBOut(__FILE__, __LINE__, s)

I wrote a blog post about this so I always knew where I could look it up: https://windowscecleaner.blogspot.co.nz/2013/04/debug-output-tricks-for-visual-studio.html

Solution 5 - C++

Use OutputDebugString instead of afxDump.

Example:

#define _TRACE_MAXLEN 500

#if _MSC_VER >= 1900
#define _PRINT_DEBUG_STRING(text) OutputDebugString(text)
#else // _MSC_VER >= 1900
#define _PRINT_DEBUG_STRING(text) afxDump << text
#endif // _MSC_VER >= 1900

void MyTrace(LPCTSTR sFormat, ...)
{
    TCHAR text[_TRACE_MAXLEN + 1];
    memset(text, 0, _TRACE_MAXLEN + 1);
    va_list args;
    va_start(args, sFormat);
    int n = _vsntprintf(text, _TRACE_MAXLEN, sFormat, args);
    va_end(args);
    _PRINT_DEBUG_STRING(text);
    if(n <= 0)
        _PRINT_DEBUG_STRING(_T("[...]"));
}

Solution 6 - C++

Even though OutputDebugString indeed prints a string of characters to the debugger console, it's not exactly like printf with regard to the latter being able to format arguments using the % notation and a variable number of arguments, something OutputDebugString does not do.

I would make the case that the _RPTFN macro, with _CRT_WARN argument at least, is a better suitor in this case -- it formats the principal string much like printf, writing the result to debugger console.

A minor (and strange, in my opinion) caveat with it is that it requires at least one argument following the format string (the one with all the % for substitution), a limitation printf does not suffer from.

For cases where you need a puts like functionality -- no formatting, just writing the string as-is -- there is its sibling _RPTF0 (which ignores arguments following the format string, another strange caveat). Or OutputDebugString of course.

And by the way, there is also everything from _RPT1 to _RPT5 but I haven't tried them. Honestly, I don't understand why provide so many procedures all doing essentially the same thing.

Solution 7 - C++

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

wstring outputMe = L"can" + L" concatenate\n";
OutputDebugString(outputMe.c_str());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionclampView Question on Stackoverflow
Solution 1 - C++SorantisView Answer on Stackoverflow
Solution 2 - C++anonView Answer on Stackoverflow
Solution 3 - C++ReunanenView Answer on Stackoverflow
Solution 4 - C++parsley72View Answer on Stackoverflow
Solution 5 - C++Maurizio TALPOView Answer on Stackoverflow
Solution 6 - C++amnView Answer on Stackoverflow
Solution 7 - C++AndrewView Answer on Stackoverflow