万恶的linux - 一个大小写引发的血案解决方法
万恶的linux -- 一个大小写引发的血案
是看下面代码:
在当前的ubuntu上运行输出的结果为:
line1 -- h
line2 -- hello world
line3 -- hello world
line4 -- ?s@!@!(乱码)
line5 -- h
line6 -- hello world
line7 -- hello world
line8 -- 无输出
只因为%s转义符小写s是针对窄字符的 大写转义符S是针对宽字符的 Linux下竟然使用这种硬编码的方式来区分宽窄字符版本
在Windows平台上上面输出的情况也有类似问题 但情况较好 Windows上小写s格式同时兼容宽窄字符模式 但在linux上 输出窄字符只能用小写 输出宽字符 只能用大写 这是一种非常悲剧的情况 这种硬编码的风格将使跨平台的成为泡沫 因为下面这段代码是在linux unicode的编译环境是十分悲剧的:
难怪有不少平台 都会实现自己的字符串函数族 但如果是不得不依赖gnu的情况下 有什么办法可以避免这种问题么 能否有人给出些建议 多谢了
------解决方案--------------------
这跟linux有啥关系, 世界上就没比M$更不遵循标准的了. linux大家都用UTF-8, UCS-2 UCS-4基本就是后娘养的, 也没见啥不方便的.
------解决方案--------------------
避免就是不用或者慎用,gcc中宽字符和普通字符流的方向还不同,不要混用
------解决方案--------------------
linux窄字符用的应该是unicode,windows默认是ANSI。
------解决方案--------------------
------解决方案--------------------
s : When used with printf functions, specifies a single-byte-character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached.
__________________________________
S : When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte-character string. Characters are printed up to the first null character or until the precision value is reached.
------解决方案--------------------
------解决方案--------------------
公平的说,Windows内核使用UTF16,宽字符,所以必须从内核层面就把宽字符的支持做好否则连内核都写不出来,因此,Windows宽字符支持明显好于Linux。
Linux使用UTF8,UTF8本身就是用char*存储的。而且,UTF8除英文外其他都是二等公民,只有在软件国际化时才会用到,内核中一个字符肯定是一个字节
------解决方案--------------------
你不用宽字符,就没有这些问题。
是看下面代码:
- C/C++ code
int main(void) { wprintf(L"%s\n", L"hello world"); // line 1 wprintf(L"%s\n", "hello world"); // line 2 wprintf(L"%S\n", L"hello world"); // line 3 wprintf(L"%S\n", "hello world"); //line 4 printf("%s\n", L"hello world"); // line 5 printf("%s\n", "hello world"); // line 6 printf("%S\n", L"hello world"); // line 7 printf("%S\n", "hello world"); //line 8 return 0; }
在当前的ubuntu上运行输出的结果为:
line1 -- h
line2 -- hello world
line3 -- hello world
line4 -- ?s@!@!(乱码)
line5 -- h
line6 -- hello world
line7 -- hello world
line8 -- 无输出
只因为%s转义符小写s是针对窄字符的 大写转义符S是针对宽字符的 Linux下竟然使用这种硬编码的方式来区分宽窄字符版本
在Windows平台上上面输出的情况也有类似问题 但情况较好 Windows上小写s格式同时兼容宽窄字符模式 但在linux上 输出窄字符只能用小写 输出宽字符 只能用大写 这是一种非常悲剧的情况 这种硬编码的风格将使跨平台的成为泡沫 因为下面这段代码是在linux unicode的编译环境是十分悲剧的:
- C/C++ code
#if defined(UNICODE) typedef wchar_t MyTchar; #define MyTprintf wprintf #define MyText(t) (L##t) #else typedef char MyTchar #define MyTprintf printf #define MyText(t) (t) #endif //.. some place MyTchar * name = getName(); MyTprintf(MyText("name=%s"), name);
难怪有不少平台 都会实现自己的字符串函数族 但如果是不得不依赖gnu的情况下 有什么办法可以避免这种问题么 能否有人给出些建议 多谢了
------解决方案--------------------
这跟linux有啥关系, 世界上就没比M$更不遵循标准的了. linux大家都用UTF-8, UCS-2 UCS-4基本就是后娘养的, 也没见啥不方便的.
------解决方案--------------------
避免就是不用或者慎用,gcc中宽字符和普通字符流的方向还不同,不要混用
------解决方案--------------------
linux窄字符用的应该是unicode,windows默认是ANSI。
------解决方案--------------------
------解决方案--------------------
s : When used with printf functions, specifies a single-byte-character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached.
__________________________________
S : When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte-character string. Characters are printed up to the first null character or until the precision value is reached.
------解决方案--------------------
------解决方案--------------------
公平的说,Windows内核使用UTF16,宽字符,所以必须从内核层面就把宽字符的支持做好否则连内核都写不出来,因此,Windows宽字符支持明显好于Linux。
Linux使用UTF8,UTF8本身就是用char*存储的。而且,UTF8除英文外其他都是二等公民,只有在软件国际化时才会用到,内核中一个字符肯定是一个字节
------解决方案--------------------
你不用宽字符,就没有这些问题。