Mapping format specifier
%s
to%ls
when_tprintf()
is mapped towprintf()
?
我使用 _T()
宏将字符串映射到 ASCII 或 Unicode,具体取决于是否定义了 _UNICODE
。
但是,像 _tprintf("%s", _T("text string"))
这样的调用给我带来了麻烦,因为定义 _UNICODE
时类型不一致。
我发现 %ls
应该用于 Unicode 字符串。
定义_UNICODE
时,如何将%s
直接映射到%ls
?有没有像_T()这样的奇特函数?
最佳答案
However, a call like
_tprintf("%s", _T("text string"))
is causing me trouble, because of inconsistent types when_UNICODE
is defined.
您还应该对 _tprintf()
的第一个字符串文字(即格式说明符字符串)使用 _T()
装饰器:
// NOTE: _T("%s"), not just "%s"
//
_tprintf(_T("%s"), _T("text string"));
这在 ANSI 版本中扩展为:
printf("%s", "text string"); // %s maps to char* ANSI string
并且在 Unicode 中构建为:
wprintf(L"%s", L"text string"); // %s maps to wchar_t* Unicode string
关于c++ - 在 C++ 中定义 Unicode 时映射格式说明符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156495/