Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2564931
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: C/C++

2013-04-04 22:54:29

    下面是书本【我的第一本c.书】-陈乔良 电子版的一些例子,实际练习了下,顺便查找了一些宽字符的例子和需要注意的地方【对于跨平台还需要考虑具体情况】,感谢网友无私奉献!嘿嘿....学习思密达...

点击(此处)折叠或打开

  1. // HelloWorld.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <fstream>
  6. #include <iomanip>

  7. using namespace std;

  8. int /*_t*/main(int argc, /*_TCHAR*/char * argv[])
  9. {
  10.     // < 基本数据
  11.     cout << "Hello World!\n";
  12.     cout << "hex " << hex << 1233 << endl;
  13.     cout << "precision " << setprecision(8) << 3.14159265 << endl;

  14.     // < 宽字符
  15.     wchar_t * cChinese = L"我是中国人";
  16.     wcout.imbue(locale("chs"));    // < 对于wcout需要设置本地语言,否则无法显示中文
  17.     wcout << cChinese << endl;

  18.     // < 宽字符串
  19.     wstring s(L"我爱");
  20.     wcout << s.c_str() << endl;
  21.     //wcout << s << endl;
  22.     cout << "你妹丫" << endl;    // < 对于cout不需要设置本地语言,也能显示中文

  23.     /** 【Thank to author】在缺省的C locale下,ofstream能正确输出中文到文件中,但不支持中文文件名;
  24.      * wofstream支持中文文件名,但不能向文件中输出中文。要解决这个问题,
  25.      * 需要在打开文件之前将全局locale设为本地语言。将全局locale设为本地语言后,
  26.      * ofstream和wofstream的问题都解决了,但 cout和wcout却不能输出中文了。
  27.      * 要让cout和wcout输出中文,需要将全局locale恢复原来的设置
  28.      */
  29.     locale &loc=locale::global(locale(locale(),"",LC_CTYPE));
  30.     ofstream ost("ofs测试文件.txt");
  31.     wofstream wofs(L"wofs测试.txt");
  32.     locale::global(loc);
  33.     ost << "test测试" << 1234 << endl;
  34.     wofs << L"Another test还是测试" <<1234 << endl;

  35.     /** 【Thank to author】缺省情况下,语句中字符串的形式是unicode形式的。
  36.      * 如果在所有输出语句之前加上如下语句将C语言的全局locale设置为本地语言(C语言中只有全局locale)
  37.      * 就可以正常输出了:
  38.      */
  39.     setlocale(LC_CTYPE, "");
  40.     printf("%s", "multibyte中文\n");    /// < 打印:multibyte中文【\n】 - 全部
  41.     printf("%s", L"multibyte中文\n"); /// < 打印:m - 两个字节
  42.     printf("%S", "unicode中文\n");        /// < 打印:乱码 - 全部
  43.     printf("%S", L"unicode中文\n");        /// < 打印:unicode中文【\n】 - 全部    
  44.     wprintf(L"%S", "multibyte中文\n");    /// < 打印:multibyte中文【\n】 - 全部
  45.     wprintf(L"%S", L"multibyte中文\n");    /// < 打印:m - 两个字节
  46.     wprintf(L"%s", "multibyte中文\n");    /// < 打印:乱码 - 全部
  47.     wprintf(L"%s", L"unicode中文\n");    /// < 打印:unicode中文【\n】 - 全部
  48.     ///----这里有个记忆方法:printf S 必须与 L对应
  49.     ///---- wprintf s 必须与 L对应
  50.     

  51.     getchar();
  52.     return 0;
  53. }
  Attention_one:【】比较细了.....

点击(此处)折叠或打开

  1. // 输出中文


  2. &nbsp;char szA[8];
  3. &nbsp;WCHAR szW[8];

  4. &nbsp;sprintf(szA, "%s", L"和平"); // 乱码,四个字节

  5. &nbsp;sprintf(szA, "%s", "和平"); // 和平


  6. &nbsp;sprintf(szA, "%S", L"和平"); // 零字节

  7. &nbsp;sprintf(szA, "%S", "和平"); // 零字节


  8. &nbsp;swprintf(szW, L"%s", L"和平"); // 和平,四个字节

  9. &nbsp;swprintf(szW, L"%s", "和平"); // 无法输出,四个字节,内容是ANSI码


  10. &nbsp;swprintf(szW, L"%S", L"和平"); // 无法输出,八个字节,内容是Unicode码

  11. &nbsp;swprintf(szW, L"%S", "和平"); // 无法输出,八个字节,内容是ANSI码



  12. &nbsp;wsprintfA(szA, "%s", L"和平"); // 乱码,四个字节

  13. &nbsp;wsprintfA(szA, "%s", "和平"); // 和平


  14. &nbsp;wsprintfA(szA, "%S", L"和平"); // 和平

  15. &nbsp;wsprintfA(szA, "%S", "和平"); // 乱码,两个字节


  16. &nbsp;wsprintfW(szW, L"%s", L"和平"); // 和平,四个字节

  17. &nbsp;wsprintfW(szW, L"%s", "和平"); // 无法输出,四个字节,内容是ANSI码


  18. &nbsp;wsprintfW(szW, L"%S", L"和平"); // 无法输出,六个字节,内容是Unicode码

  19. &nbsp;wsprintfW(szW, L"%S", "和平"); // 和平,八个字节

  20. &nbsp;

  21. // 输出英文


  22. &nbsp;char szA[8];
  23. &nbsp;WCHAR szW[8];

  24. &nbsp;sprintf(szA, "%s", L"well"); // w,一个字节

  25. &nbsp;sprintf(szA, "%s", "well"); // well,四个字节


  26. &nbsp;sprintf(szA, "%S", L"well"); // well,四个字节

  27. &nbsp;sprintf(szA, "%S", "well"); // 零字节


  28. &nbsp;swprintf(szW, L"%s", L"well"); // well,八个字节

  29. &nbsp;swprintf(szW, L"%s", "well"); // 乱码,四个字节


  30. &nbsp;swprintf(szW, L"%S", L"well"); // w,两个字节

  31. &nbsp;swprintf(szW, L"%S", "well"); // well,八个字节



  32. &nbsp;wsprintfA(szA, "%s", L"well"); // w,一个字节

  33. &nbsp;wsprintfA(szA, "%s", "well"); // well,四个字节


  34. &nbsp;wsprintfA(szA, "%S", L"well"); // well,四个字节

  35. &nbsp;wsprintfA(szA, "%S", "well"); // 乱码,四个字节


  36. &nbsp;wsprintfW(szW, L"%s", L"well"); // well,八个字节

  37. &nbsp;wsprintfW(szW, L"%s", "well"); // 乱码,四个字节,内容是ANSI码


  38. &nbsp;wsprintfW(szW, L"%S", L"well"); // w,两个字节

  39. &nbsp;wsprintfW(szW, L"%S", "well");

阅读(1176) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~