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

新手上路,请多包涵

我已经尝试过 OutputDebugString 功能,但大多数时候我会收到如下错误:

错误 C2664:“OutputDebugStringA”:无法将参数 1 从“int”转换为“LPCSTR”

例子

尝试1:

 //ERROR: sprintf is unsafe. Use sprintf_s instead
int x = 4;
char s[256];
sprintf(s, "There is %d numbers", x);
OutputDebugString((LPCWSTR)s);

尝试2:

 //FAIL: outputs junk (sprintf_s doesn't understand unicode?)
int x = 4;
char s[256];
sprintf_s(s, "There is %d numbers", x);
OutputDebugString((LPCWSTR)s);

尝试 3:

 //ERROR: no instance of overloaded function "sprintf_s" matches the argument list
int x = 4;
TCHAR s[256];
sprintf_s(s, "There is %d numbers", x);
OutputDebugString((LPCWSTR)s);

尝试4:

 //ERROR: no instance of overloaded function "sprintf_s" matches the argument list
int x = 4;
TCHAR s[256];
sprintf_s(s, L"There is %d numbers", x);
OutputDebugString((LPCWSTR)s);

尝试 5:

 //ERROR: no instance of overloaded function "swprintf" matches the argument list
int x = 4;
TCHAR s[256];
swprintf(s, "There is %d numbers", x);
OutputDebugString(s);

尝试 6:

 //ERROR: 'swprintf': function has been changed to confirm with the ISO C standard, adding an extra character count parameter
int x = 4;
TCHAR s[256];
swprintf(s, L"There is %d numbers", x);
OutputDebugString(s);

原文由 understack 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 858
2 个回答

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

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

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

原文由 peter_mcc 发布,翻译遵循 CC BY-SA 2.5 许可协议

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

 OutputDebugString("This is an output");

原文由 Donotalo 发布,翻译遵循 CC BY-SA 2.5 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题