gpt4 book ai didi

c++ - 在 VS2017 中为 OutputDebugString 使用整数

转载 作者:行者123 更新时间:2023-11-30 05:03:18 25 4
gpt4 key购买 nike

我在网上搜索了一段时间如何使用 OutputDebugString() 输出整数或可选的 float 。如果我可以使用此命令将我的调试数据从我的外部可执行文件直接写入控制台,那会更容易。但是我只让它与 const char 一起工作。我找到的解决方案已过时 我尝试直接从网上复制和粘贴代码,但没有用。即使修改了代码,我也无法正确地进行类型转换。有没有人可以帮助我尽可能干净地将某些内容类型转换为 OutputDebugString,它仅用于调试目的,所以我宁愿保持代码简短且易于阅读,也不愿使用更复杂和笨重的类型转换 IF 。非常感谢!

最佳答案

提供了替代解决方案。下面的函数封装了可以接受格式化参数的 OutputDebugString。

#include <vector>
#include <string>
void DbgMsg(const char * zcFormat, ...)
{
// initialize use of the variable argument array
va_list vaArgs;
va_start(vaArgs, zcFormat);

// reliably acquire the size
// from a copy of the variable argument array
// and a functionally reliable call to mock the formatting
va_list vaArgsCopy;
va_copy(vaArgsCopy, vaArgs);
const int iLen = std::vsnprintf(NULL, 0, zcFormat, vaArgsCopy);
va_end(vaArgsCopy);

// return a formatted string without risking memory mismanagement
// and without assuming any compiler or platform specific behavior
std::vector<char> zc(iLen + 1);
std::vsnprintf(zc.data(), zc.size(), zcFormat, vaArgs);
va_end(vaArgs);
std::string strText(zc.data(), iLen);

OutputDebugStringA(strText.c_str());
}

例如,下面的代码显示了如何通过 OutputDebugString 通过 DbgMsg() 打印整数变量。

int foo=12;
DbgMsg(" foo=%d", foo);

关于c++ - 在 VS2017 中为 OutputDebugString 使用整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49471983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com