windows printf 未知说明符 %S
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7475452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
printf unknown specifier %S
提问by mugetsu
I am trying to get a sample code working and it uses the following lines:
我正在尝试使示例代码正常工作,它使用以下几行:
WCHAR cBuf[MAX_PATH];
GetSharedMem(cBuf, MAX_PATH);
printf("Child process read from shared memory: %S\n", cBuf);
get shared mem:
获取共享内存:
__declspec(dllexport) VOID __cdecl GetSharedMem(LPWSTR lpszBuf, DWORD cchSize)
{
LPWSTR lpszTmp;
// Get the address of the shared memory block
lpszTmp = (LPWSTR) lpvMem;
// Copy from shared memory into the caller's buffer
while (*lpszTmp && --cchSize)
*lpszBuf++ = *lpszTmp++;
*lpszBuf = ' Windows function Specifier Parameter needs to be
printf/sprintf (single/MBCS) %S wchar_t*
_tprintf/_stprintf (generic) %S (don't use)
wprintf/swprintf (wide) %S char*
ANSI function Specifier Parameter needs to be
printf/sprintf (single/MBCS) %S wchar_t*
wprintf/swprintf (wide) %S wchar_t*
';
}
strangely I get an unknown specifier error on the printf line. %S is a MS extension and not ANSI compatible but I would've thought that it would be included by default. How do I turn this on?
奇怪的是,我在 printf 行上收到了一个未知的说明符错误。%S 是 MS 扩展名,与 ANSI 不兼容,但我认为默认情况下会包含它。我如何打开这个?
I'm using microsoft visual studios, not sure how to check my options
我正在使用 microsoft visual studios,不知道如何检查我的选项
回答by nullforce
From http://www.globalyzer.com/gzserver/help/localeSensitiveMethods/formatting.htm#larges
来自http://www.globalyzer.com/gzserver/help/localeSensitiveMethods/formatting.htm#larges
Unqualified String Specifiers (large %S)
不合格的字符串说明符(大 %S)
These tables show how Windows and ANSI treat parameters based on the %S specifier and the style of function call (single, generic, or wide):
这些表显示了 Windows 和 ANSI 如何根据 %S 说明符和函数调用样式(单一、通用或宽)处理参数:
#include "stdafx.h"
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
WCHAR cBuf[MAX_PATH];
TCHAR tBuf[MAX_PATH];
wcsncpy_s(cBuf, L"Testing\r\n", MAX_PATH);
_tcsncpy_s(tBuf, _T("Testing\r\n"), MAX_PATH);
printf("%S", cBuf); // Microsoft extension
printf("%ls", cBuf); // works in VC++ 2010
wprintf(L"%s", cBuf); // wide
_tprintf(_T("%s"), tBuf); // single-byte/wide
return 0;
}
Both ANSI and Windows treat %S basically as opposite of %s in terms of single byte or wide, which ironically means that Windows and ANSI again handle these specifiers differently.
ANSI 和 Windows 在单字节或宽度方面基本上将 %S 视为与 %s 的对立面,具有讽刺意味的是,Windows 和 ANSI 再次以不同的方式处理这些说明符。
Note that ANSI in essence always treats %S in the same way as %ls, in other words it is always assumed to be wide string.
请注意,ANSI 本质上始终以与 %ls 相同的方式对待 %S,换句话说,它始终被假定为宽字符串。
Windows on the other hand treats %S differently based on the type of function call. For single byte function calls, %S acts like the wide %ls specifier, but for wide functions calls, %S acts like the single byte %hs specifier.
另一方面,Windows 根据函数调用的类型对 %S 进行不同的处理。对于单字节函数调用,%S 的作用类似于宽 %ls 说明符,但对于宽函数调用,%S 的作用类似于单字节 %hs 说明符。
This specifier should not be used for Windows Generic calls. Since %S is the "opposite" of %s, the parameter would need to be wide if the _UNICODE flag is off, and single byte if the _UNICODE flag is on. The TCHAR generic type does not work this way, and there's not "anti-TCHAR" kind of datatype.
此说明符不应用于 Windows 通用调用。由于 %S 是 %s 的“相反”,如果 _UNICODE 标志关闭,则参数需要是宽的,如果 _UNICODE 标志打开,则参数需要是单字节的。TCHAR 泛型类型不能以这种方式工作,并且没有“反 TCHAR”类型的数据类型。
I tried the following in Visual C++ 2010:
我在 Visual C++ 2010 中尝试了以下操作:
wchar_t * str = /*...*/
printf("The string: %ls\n", str);
// or
wprintf(L"The string: %ls\n", str);
Settings:
设置:
/ZI /nologo /W3 /WX- /Od /Oy- /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /GS /fp:precise /Zc:wchar_t /Zc:forScope /Yu"StdAfx.h" /Fp"Debug\TestWchar.pch" /Fa"Debug\" /Fo"Debug\" /Fd"Debug\vc100.pdb" /Gd /analyze- /errorReport:queue
/ZI /nologo /W3 /WX- /Od /Oy- /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /GS /fp:精确 /Zc:wchar_t /Zc:forScope /Yu"StdAfx.h" /Fp"Debug\TestWchar.pch" /Fa"Debug\" /Fo"Debug\" /Fd"Debug\vc100.pdb" /Gd /analyze - /错误报告:队列
回答by Kerrek SB
To print a wide string:
要打印宽字符串:
##代码##