+ -
当前位置:首页 → 问答吧 → 求助:向文件中输出中文,使用wfstream,imbuse()运行时报错

求助:向文件中输出中文,使用wfstream,imbuse()运行时报错

时间:2010-07-09

来源:互联网

rt
程序意图: 向文件中输出中文
见代码
  1. /输出到文件中查看
  2.                 wofstream woutfile("wout.log" , ios::out|ios::app|ios::ate);
  3.                 woutfile<<L"will write woutfile"<<endl;
  4.                 woutfile<<L"将开始写"<<endl;
  5.                 std::locale oldloc = woutfile.getloc();
  6.                 woutfile.imbue(std::locale("chs"));
  7.                 woutfile<<L"woutfile ws = "<<ws<<endl;
  8.                 woutfile.imbue(oldloc);
复制代码
运行时报错:
terminate called after throwing an instance of 'std::runtime_error'
  what():  locale::facet::_S_create_c_locale name not valid
Aborted

作者: AssassinPig   发布时间: 2010-07-09

"chs"无效,用"zh_CN.UTF-8"试试

作者: 没本   发布时间: 2010-07-09

zh_CN.UTF-8 是echo $LANG查看的那些么?
怎么看系统支不支持zh_CN.UTF-8?

作者: AssassinPig   发布时间: 2010-07-09

用 locale -a 看。

你得让glibc支持你的语言才行。如果不支持。

编辑/etc/locale.gen
  1. en_US ISO-8859-1
  2. en_US.UTF-8 UTF-8
  3. zh_CN.GBK GBK
  4. zh_CN.GB2312 GB2312
  5. zh_CN.GB18030 GB18030
  6. zh_CN.UTF-8 UTF-8
复制代码
然后 sudo locale-gen

作者: 没本   发布时间: 2010-07-09

至于环境变量我的机子设置了这几个
  1. LC_MESSAGES=zh_CN.UTF-8
  2. LANG=zh_CN.UTF-8
  3. LC_CTYPE=zh_CN.UTF-8
复制代码

作者: 没本   发布时间: 2010-07-09