【问题标题】:Simplest way to write output message to 'output window' in Visual Studio 2010?在 Visual Studio 2010 中将输出消息写入“输出窗口”的最简单方法?
【发布时间】:2010-07-05 11:45:59
【问题描述】:

我已经尝试过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);

【问题讨论】:

    标签: c++ visual-studio visual-studio-2010


    【解决方案1】:

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

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

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

    【讨论】:

    • + @Jon: 更好的是,考虑使用 std::stringstream。
    • @Billy ONeal:我使用 std::stringstream 而不是 sprintf/sprintf_s 正如你所建议的那样。谢谢。
    • @BillyONEal 你能解释一下为什么使用 stringstream 会更好吗?只是好奇。
    • @TrevorHart 2017 年法案与 2010 年法案并不完全一致。主要优点是无需担心目标缓冲区大小。但是 sprintf 有很多用途:)
    • @BillyONeal 你会如何使用std::stringstream?语法是什么?
    【解决方案2】:

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

    例如,

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

    【讨论】:

      【解决方案3】:

      我知道的最常见的方式是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 );
      

      【讨论】:

      • 好像TRACE是在MFC中定义的,我没用过。
      【解决方案4】:

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

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

      OutputDebugString(L"test\n");
      

      它对我很有用。

      编辑:

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

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

      我绝不是专家,我只是发现了一些可行的方法并继续前进。

      【讨论】:

      • 如果我想格式化字符串并将数据传递给它,这并没有考虑到。
      • 我添加了适用于注射的解决方案。我专攻iOS平台和语言,请不要将此视为专家解决方案:)
      • 根据您的编译器选项和设置,OutputDebugString 被转换为两个目标之一,OutputDebugStringW 用于宽文本字符 (wchart_t) 或 OutputDebugStringA 用于窄文本字符 (char )。 L 是创建wchar_t 宽字符串所必需的,如L"wide char""narrow char"sprintf_s() 函数使用窄字符,标准char 类型,因此char buffer[100]; 内容使用OutputDebugStringA() 打印到输出窗口以强制使用窄字符文本。
      【解决方案5】:

      用途:

      OutputDebugStringA("Some random text");
      

      或者:

      OutputDebugString("Some random text");
      

      【讨论】:

        【解决方案6】:

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

        OutputDebugString("This is an output");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-24
          • 2019-02-01
          • 1970-01-01
          • 2012-03-21
          • 1970-01-01
          • 2010-11-12
          相关资源
          最近更新 更多