gpt4 book ai didi

c++ - 使用 StringCchPrintf 从子字符串构建字符串

转载 作者:行者123 更新时间:2023-11-30 04:23:53 27 4
gpt4 key购买 nike

我正在尝试从来自错误消息的子字符串构建字符串:

// the error message
const char* error_msg = e.what();

size_t const cchDest = 100;
TCHAR pszDest[cchDest];

LPCTSTR pszFormat = TEXT("%s %s");
TCHAR* pszTxt = TEXT("The error is: ");

HRESULT hr = StringCchPrintf(pszDest, cchDest, pszFormat, pszTxt, error_msg );

我希望第二个 %s 将被替换为 error_msg 的值,但是输出是:

错误是:☐☐a

我要如何修改上面的代码才能显示子字符串?

编辑1我也尝试了以下方法,但我得到的只是一个盒子。

TCHAR* pszTxt = TEXT("The error is: %c", error_msg );
HRESULT hr = StringCchPrintf(pszDest, cchDest, pszTxt);

最佳答案

这个有效:

LPCTSTR pszFormat = TEXT("%s %hs");
TCHAR* pszTxt = TEXT("The error is: ");

HRESULT hr = StringCchPrintf(pszDest, cchDest, pszFormat, pszTxt, error_msg);

关于c++ - 使用 StringCchPrintf 从子字符串构建字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13138936/

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