格式化输出中的%s和%S的区别
請看MSDN:http://msdn.microsoft.com/zh-cn/library/hf4y5e3w(v=vs.90).aspx
的解釋。
?
?
s
String?
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
String
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.
?
使用s時,printf是針對單字節字符的字符串,而wprintf是針對寬字符的
使用S時,正好相反,printf針對寬字符
?
CString中的format與printf類似,在unicode字符集的工程中,使用
CString str1, str2;
str1.format(_T("%S"), str2);
時
%S專指單字節字符的字符串,而str2為寬字符,類型不匹配,故出現不可預期的錯誤。
?
若str2為英文字符,如“abcd”,就只能輸出a,因str2為寬字符,a有兩個字節,值為0x0061,在內存中為61 00,故按單字節輸出只能輸出61,碰到00,即空字符后認為字符串結束,不會再輸出。
若str2為中文字符,中文字符一般會占滿兩字節,而按單字節字符就會按一個字節一個字節的輸出,故會輸出亂碼。
?
總結
以上是生活随笔為你收集整理的格式化输出中的%s和%S的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决AD 不能打开DDB文件的解决方案
- 下一篇: 完美解决LINK : fatal err