LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 732|回复: 1

如何使用wcin?

[复制链接]
发表于 2003-11-29 16:04:38 | 显示全部楼层 |阅读模式
下面的代码,我想把输入的字符串转化为宽字符型。

  1. #include<string>
  2. #include<iostream>
  3. using namespace std;
  4. int
  5. main()
  6. {
  7.     wstring str;
  8.     wcin.imbue(locale("zh_CN.GBK"));
  9.     wcin >> str;
  10.     wcout << L"size =" << str.size() << endl;
  11.     wcout << L"str =" << str << endl;
  12.     return 0;
  13. }
复制代码

使用g++ -o test test.cpp -D_LIBCPP_WCHAT_T 编译
在windows中使用dev-cpp编译通不过(wchar.t中的vwscanf没有声明),在linux中编译通过,但是运行却有问题:不能输入中文。
>./test
中文测试
>size = 0
str =
>./test
中文测试123
>size = 0
str =
>./test
123中文测试
>size = 3
str = 123
发表于 2003-12-1 13:29:52 | 显示全部楼层
wcin.imbue(locale("zh_CN.GBK"));


使用wcin<<直接读不进
不知道是控制台对8位输入进行了转义 或是wcin不支持8位字符

你可以使用getline(wcin,str);看
看结果会怎样
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表