在Visual Studio 2010中将输出消息写入"输出窗口"的最简单方法是什么?

und*_*ack 17 c++ visual-studio-2010

我试过OutputDebugString功能,大多数时候我得到的错误如下:

error C2664: 'OutputDebugStringA' : cannot convert parameter 1 from 'int' to 'LPCSTR'
Run Code Online (Sandbox Code Playgroud)

请建议.谢谢.

pet*_*mcc 24

它只接受字符串作为参数,而不是整数.尝试类似的东西

sprintf(msgbuf, "My variable is %d\n", integerVariable);
OutputDebugString(msgbuf);
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看http://www.unixwiz.net/techtips/outputdebugstring.html

  • 考虑使用sprintf_s (7认同)
  • + @Jon:更好的是,考虑使用std :: stringstream. (5认同)

Kir*_*sky 11

For debugging purposes you could use _RPT macros.

For instance,

_RPT1( 0, "%d\n", my_int_value );
Run Code Online (Sandbox Code Playgroud)


Inv*_*rse 9

我所知道的最常见的方式是TRACE宏:

http://msdn.microsoft.com/en-us/library/4wyz8787%28VS.80%29.aspx

例如:

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 );
Run Code Online (Sandbox Code Playgroud)


小智 5

我在搜索错误消息时找到了这个答案:https : //stackoverflow.com/a/29800589

基本上,您只需要在使用时在输出字符串前面放一个“L” OutputDebugString

OutputDebugString(L"test\n");
Run Code Online (Sandbox Code Playgroud)

它对我很有用。

编辑:

为了用数据格式化字符串,我最终使用了

char buffer[100];
sprintf_s(buffer, "check it out: %s\n", "I can inject things");
OutputDebugStringA(buffer);
Run Code Online (Sandbox Code Playgroud)

我绝不是专家,我只是找到了一些有用的东西并继续前进。

  • 根据您的编译器选项和设置,`OutputDebugString` 将被转换为两个目标之一:用于宽文本字符 (`wchart_t`) 的 `OutputDebugStringW` 或用于窄文本字符 (`char`) 的 `OutputDebugStringA`。创建“wchar_t”宽字符串需要“L”,如“L"wide char"”与“narrow char”相比。`sprintf_s()` 函数使用窄字符,标准 `char` 类型,因此使用 `OutputDebugStringA()` 将 `char buffer[100];` 内容打印到输出窗口,以强制使用窄字符文本。 (2认同)