visual studio swprintf使所有我的%s格式化程序都希望使用wchar_t *而不是char * [英] visual studio swprintf is making all my %s formatters want wchar_t * instead of char *

查看:377
本文介绍了visual studio swprintf使所有我的%s格式化程序都希望使用wchar_t *而不是char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多平台项目,可以在Mac上很好地编译,但是在Windows上,我所有带有%s的swprintf调用都在寻找wchar_t而不是char * im传递了它.原来,M $认为让%s代表宽字符功能中的char *以外的东西会很有趣...

Ive got a multi platform project that compiles great on mac, but on windows all my swprintf calls with a %s are looking for a wchar_t instead of the char * im passing it. Turns out M$ thought it would be funny to make %s stand for something other than char * in wide character functions... http://msdn.microsoft.com/en-us/library/hf4y5e3w.aspx

无论如何,我一直在寻找一种创新的编码技巧,它比将ifdef结束于每个宽字符串调用周围要好

Anyways I'm looking for a creative coding trick thats better than putting ifdef else ends around every wide string call

推荐答案

更新:VS 2015 CTP6恢复了更改,Microsoft再次表现出与标准不同的作用.

Update: VS 2015 CTP6 reverted the changes and Microsoft are yet again acting different to the standard.

sprintf %Tschar*,对于swprintf %Tswchar_t*.

Visual Studio 14 CTP1 and later will always treat %s as a narrow string (char*) unless you define _CRT_STDIO_LEGACY_WIDE_SPECIFIERS. It also added the T length modifier extension which maps to what MS calls the "natural" width. For sprintf %Ts is char* and for swprintf %Ts is wchar_t*.

在Visual Studio 13和更低版本中,%s/%c映射到函数/格式字符串的自然宽度,而%S/%C映射到与自然函数相反的宽度,

In Visual Studio 13 and earlier %s/%c is mapped to the natural width of the function/format string and %S/%C is mapped to the opposite of the natural with:

printf("%c %C %s %S\n", 'a', L'B', "cd", L"EF");
wprintf(L"%c %C %s %S\n", L'a', 'B', L"cd", "EF");

还可以使用长度修饰符来强制指定宽度:%ls%lc%ws%wc始终表示wchar_t%hs%hc始终为char. (针对VS2003的文档此处和VC6 此处(不确定%ws及其真正添加的时间))

You can also force a specific width by using a length modifier: %ls, %lc, %ws and %wc always mean wchar_t and %hs and %hc are always char. (Documented for VS2003 here and VC6 here (Not sure about %ws and when it was really added))

通过使用

Mapping %s to the natural width of the function was really handy back in the days of Win9x vs. WinNT, by using the tchar.h header you could build narrow and wide releases from the same source. When _UNICODE is defined the functions in tchar.h map to the wide functions and TCHAR is wchar_t, otherwise the narrow functions are used and TCHAR is char:

_tprintf(_T("%c %s\n"), _T('a'), _T("Bcd"));

Windows SDK头文件使用了类似的约定,并且那里存在一些格式函数(wsprintf,wvsprintf,wnsprintf和wvnsprintf),但是它们由UNICODETEXT而不是_UNICODE_T/_TEXT.

There is a similar convention used by the Windows SDK header files and the few format functions that exist there (wsprintf, wvsprintf, wnsprintf and wvnsprintf) but they are controlled by UNICODE and TEXT and not _UNICODE and _T/_TEXT.

如果要支持较旧的Windows编译器,则可能有3种选择可以使多平台项目在Windows上运行:

You probably have 3 choices to make a multi-platform project work on Windows if you want to support older Windows compilers:

1)在Windows上作为一个狭窄的字符串项目进行编译,可能不是一个好主意,在您的情况下,swprintf仍会将%s视为wchar_t *.

1) Compile as a narrow string project on Windows, probably not a good idea and in your case swprintf will still treat %s as wchar_t*.

2):使用自定义定义类似于inttypes.h格式字符串的工作方式:

2) Use custom defines similar to how inttypes.h format strings work:

#ifdef _WIN32
#define PRIs "s"
#define WPRIs L"hs"
#else
#define PRIs "s"
#define WPRIs L"s" 
#endif
printf("%" PRIs " World\n", "Hello");
wprintf(L"%" WPRIs L" World\n", "Hello");

3)创建自己的swprintf自定义版本,并将其与Visual Studio 13及更早版本一起使用.

3) Create your own custom version of swprintf and use it with Visual Studio 13 and earlier.

这篇关于visual studio swprintf使所有我的%s格式化程序都希望使用wchar_t *而不是char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆