无法使用 swprintf 在 WCHAR* 中打印 char*
Can't print char* in WCHAR* with swprintf
我有这个功能(我只需要在最后将字符串转换为 WHAT*):
wstring stringtowstring(string str)
{
WCHAR ch[256];
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)ch, 256);
return wstring(ch);
}
但我想知道为什么没有:
WCHAR tmp[256];
swprintf(tmp, L"%s", "test");
假设在上下文中 Visual Studio,这是 swprintf
的 documented non-standard behavior。
Character and string arguments that are specified by using c and s are interpreted as char and char* by printf family functions, or as wchar_t and wchar_t* by wprintf family functions. Character and string arguments that are specified by using C and S are interpreted as wchar_t and wchar_t* by printf family functions, or as char and char* by wprintf family functions. This behavior is Microsoft-specific.
Microsoft-specific:
The Z type character, and the behavior of the c, C, s, and S type characters when they are used with the printf and wprintf functions, are Microsoft extensions. The ISO C standard uses c and s consistently for narrow characters and strings, and C and S for wide characters and strings, in all formatting functions.
我有这个功能(我只需要在最后将字符串转换为 WHAT*):
wstring stringtowstring(string str)
{
WCHAR ch[256];
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, (LPWSTR)ch, 256);
return wstring(ch);
}
但我想知道为什么没有:
WCHAR tmp[256];
swprintf(tmp, L"%s", "test");
假设在上下文中 Visual Studio,这是 swprintf
的 documented non-standard behavior。
Character and string arguments that are specified by using c and s are interpreted as char and char* by printf family functions, or as wchar_t and wchar_t* by wprintf family functions. Character and string arguments that are specified by using C and S are interpreted as wchar_t and wchar_t* by printf family functions, or as char and char* by wprintf family functions. This behavior is Microsoft-specific.
Microsoft-specific: The Z type character, and the behavior of the c, C, s, and S type characters when they are used with the printf and wprintf functions, are Microsoft extensions. The ISO C standard uses c and s consistently for narrow characters and strings, and C and S for wide characters and strings, in all formatting functions.