How to write to the Output window in Visual Studio?

前端 未结 7 2032
小鲜肉
小鲜肉 2021-01-30 02:39

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

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

相关标签:
7条回答
  • 2021-01-30 03:29

    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);
    }
    
    0 讨论(0)
  • 2021-01-30 03:32

    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() );  \
    }
    
    0 讨论(0)
  • 2021-01-30 03:35
    #define WIN32_LEAN_AND_MEAN
    #include <Windows.h>
    
    wstring outputMe = L"can" + L" concatenate\n";
    OutputDebugString(outputMe.c_str());
    
    0 讨论(0)
  • 2021-01-30 03:41

    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.

    0 讨论(0)
  • 2021-01-30 03:42

    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 );
    
    0 讨论(0)
  • 2021-01-30 03:44

    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

    0 讨论(0)
提交回复
热议问题