C++ 在 Visual Studio 2010 中将输出消息写入“输出窗口”的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3179199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:15:45  来源:igfitidea点击:

Simplest way to write output message to 'output window' in Visual Studio 2010?

c++visual-studiovisual-studio-2010

提问by understack

I've tried OutputDebugStringfunction and most of the time I get error like :

我已经尝试过OutputDebugString函数,但大部分时间我都会收到类似的错误:

error C2664: 'OutputDebugStringA' : cannot convert parameter 1 from 'int' to 'LPCSTR'

Please suggest. Thanks.

请建议。谢谢。

回答by peter_mcc

It only accepts a string as a parameter, not an integer. Try something like

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

sprintf(msgbuf, "My variable is %d\n", integerVariable);
OutputDebugString(msgbuf);

For more info take a look at http://www.unixwiz.net/techtips/outputdebugstring.html

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

回答by Kirill V. Lyadvinsky

For debugging purposes you could use _RPTmacros.

出于调试目的,您可以使用_RPT

For instance,

例如,

_RPT1( 0, "%d\n", my_int_value );

回答by Inverse

The most common way I'm aware of is the TRACEmacro:

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

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

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

For example:

例如:

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

回答by Jangles

I found this answer when searching the error message: https://stackoverflow.com/a/29800589

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

Basically, you just need put an "L" in front of your output string when using OutputDebugString:

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

OutputDebugString(L"test\n");

OutputDebugString(L"test\n");

It worked great for me.

它对我很有用。

Edit:

编辑:

For formatting strings with data, I ended up using

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

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

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

By no means am I an expert, I just found something that worked and moved on.

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

回答by Kingeh

Use:

用:

OutputDebugStringA("Some random text");

Or:

或者:

OutputDebugString("Some random text");

回答by Donotalo

To use OutputDebugString(), provide char *or const char *as parameter:

要使用 OutputDebugString(),请提供char *const char *作为参数:

OutputDebugString("This is an output");