c++ - 在 C++ 中定义 Unicode 时映射格式说明符?

标签 c++ windows format-specifiers tchar

Mapping format specifier %s to %ls when _tprintf() is mapped to wprintf()?

我使用 _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/

相关文章:

c++ - QDateTime Isodate with timezone ?

c++ - 段错误发生在某些编译器中,但其他编译器没有

c++ - 将 boost::shared_array<void> 转换为 boost::shared_array<int>

c# - 如何防止 Windows 操作系统进入休眠模式 - .Net

sql - 如何将图像字段导出到文件?

c - 函数没有返回正确的值

c++ - 为什么 myString.at(myString.length()) 不起作用?

python - 来自 Python 的 Win32 长路径

swift - Swift 中字符串常量的字符串格式化程序?

c - 使用宏转换打印语句