免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4202 | 回复: 1
打印 上一主题 下一主题

请问,wcout 输出 wchar_t 字符串的具体工作过程?(已经解决) [复制链接]

论坛徽章:
24
狮子座
日期:2013-12-31 10:48:0015-16赛季CBA联赛之吉林
日期:2016-04-18 14:43:1015-16赛季CBA联赛之北控
日期:2016-05-18 15:01:4415-16赛季CBA联赛之上海
日期:2016-06-22 18:00:1315-16赛季CBA联赛之八一
日期:2016-06-25 11:02:2215-16赛季CBA联赛之佛山
日期:2016-08-17 22:48:2615-16赛季CBA联赛之福建
日期:2016-12-27 22:39:272016科比退役纪念章
日期:2017-02-08 23:49:4315-16赛季CBA联赛之八一
日期:2017-02-16 01:05:3415-16赛季CBA联赛之山东
日期:2017-02-22 15:34:5615-16赛季CBA联赛之上海
日期:2017-11-25 16:17:5015-16赛季CBA联赛之四川
日期:2016-01-17 18:38:37
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-14 11:04 |只看该作者 |倒序浏览
各位前辈,请问 wcout 输出 wchar_t 字符串的具体工作过程是什么样的?
我正在调试我的 win2k/mingw 环境下 STLport 中的 wcout ,现在 wcout 输出中文 wchar_t 字符串乱码。
我用 gdb 调试发现 wcout 输出 wchar_t 字符串时,先调用 codecvt  把 wchar_t 字符串转换成 char 字符串,然后输出 char 字符串。但是在我的 mingw 环境中转换中文时好像不对。

[ 本帖最后由 zhujiang73 于 2006-11-15 11:28 编辑 ]

论坛徽章:
24
狮子座
日期:2013-12-31 10:48:0015-16赛季CBA联赛之吉林
日期:2016-04-18 14:43:1015-16赛季CBA联赛之北控
日期:2016-05-18 15:01:4415-16赛季CBA联赛之上海
日期:2016-06-22 18:00:1315-16赛季CBA联赛之八一
日期:2016-06-25 11:02:2215-16赛季CBA联赛之佛山
日期:2016-08-17 22:48:2615-16赛季CBA联赛之福建
日期:2016-12-27 22:39:272016科比退役纪念章
日期:2017-02-08 23:49:4315-16赛季CBA联赛之八一
日期:2017-02-16 01:05:3415-16赛季CBA联赛之山东
日期:2017-02-22 15:34:5615-16赛季CBA联赛之上海
日期:2017-11-25 16:17:5015-16赛季CBA联赛之四川
日期:2016-01-17 18:38:37
2 [报告]
发表于 2006-11-15 11:13 |只看该作者

问题基本解决,看来我的判断是对的。

gdb 真是好工具,没有 gdb 我真没办法了。 经过 gdb 跟踪,问题锁定在 STLport 中 的  _fstream.c  文件中的

  1. template <class _CharT, class _Traits>  __BF_int_type__  basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
复制代码
和 codecvt.cpp 的

  1. codecvt<wchar_t, char, mbstate_t>::result
  2. codecvt<wchar_t, char, mbstate_t>::do_out(state_type&         /* state */,
  3.                                           const intern_type*  from,
  4.                                           const intern_type*  from_end,
  5.                                           const intern_type*& from_next,
  6.                                           extern_type*        to,
  7.                                           extern_type*        to_limit,
  8.                                           extern_type*&       to_next) const
复制代码

函数中。

现在我把 codecvt<wchar_t, char, mbstate_t>::do_out 改为

  1. codecvt<wchar_t, char, mbstate_t>::result
  2. codecvt<wchar_t, char, mbstate_t>::do_out(state_type&         /* state */,
  3.                                           const intern_type*  from,
  4.                                           const intern_type*  from_end,
  5.                                           const intern_type*& from_next,
  6.                                           extern_type*        to,
  7.                                           extern_type*        to_limit,
  8.                                           extern_type*&       to_next) const {

  9.   ptrdiff_t len = (min) (from_end - from, to_limit - to);

  10.   int len_str;
  11.   wchar_t *pwch = (wchar_t*)(from+len);
  12.   *pwch = L'\0';
  13.   //pwch = (wchar_t*)from;
  14.   pwch = (wchar_t*)from;
  15.   
  16.   len_str = wcstombs(NULL, pwch, 0);

  17.   //printf("len_str = %d\n", len_str);
  18.   //len = len_str;
  19.   wcstombs(to, from, len_str);
  20.   *(to+len_str)='\0';
  21.   
  22.   from_next = from + len;
  23.   to_next   = to   + len_str;
  24.   
  25.   return ok;
  26. }
复制代码

把  _fstream.c  文件中的

  1. template <class _CharT, class _Traits>  __BF_int_type__  basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
复制代码

改为

  1. template <class _CharT, class _Traits>
  2. __BF_int_type__
  3. basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
  4.   // Switch to output mode, if necessary.
  5.   if (!_M_in_output_mode)
  6.     if (!_M_switch_to_output_mode())
  7.       return traits_type::eof();

  8.   _CharT* __ibegin = this->_M_int_buf;
  9.   _CharT* __iend   = this->pptr();
  10.   this->setp(_M_int_buf, _M_int_buf_EOS - 1);

  11.   // Put __c at the end of the internal buffer.
  12.   if (!traits_type::eq_int_type(__c, traits_type::eof()))
  13.     *__iend++ = _Traits::to_char_type(__c);

  14.   // For variable-width encodings, output may take more than one pass.
  15.   while (__ibegin != __iend) {
  16.     const _CharT* __inext = __ibegin;
  17.     char* __enext         = _M_ext_buf;
  18.     typename _Codecvt::result __status
  19.       = _M_codecvt->out(_M_state, __ibegin, __iend, __inext,
  20.                         _M_ext_buf, _M_ext_buf_EOS, __enext);
  21.     if (__status == _Codecvt::noconv) {
  22.       return _Noconv_output<_Traits>::_M_doit(this, __ibegin, __iend)
  23.         ? traits_type::not_eof(__c)
  24.         : _M_output_error();
  25.     }

  26.     // For a constant-width encoding we know that the external buffer
  27.     // is large enough, so failure to consume the entire internal buffer
  28.     // or to produce the correct number of external characters, is an error.
  29.     // For a variable-width encoding, however, we require only that we
  30.     // consume at least one internal character
  31.    
  32.     else if (__status != _Codecvt::error &&
  33.              (((__inext == __iend /*__iend = (_CharT*)__inext*/) &&
  34.                (/*__enext - _M_ext_buf == _M_width * (__iend - __ibegin)*/ true)) ||
  35.               (!_M_constant_width && __inext != __ibegin)))
  36.    
  37.     {
  38.         // We successfully converted part or all of the internal buffer.
  39.       ptrdiff_t __n = __enext - _M_ext_buf;

  40.       
  41.       if (_M_write(_M_ext_buf, __n))
  42.         __ibegin += __inext - __ibegin;
  43.       else
  44.         return _M_output_error();
  45.     }
  46.     else
  47.       return _M_output_error();
  48.   }

  49.   return traits_type::not_eof(__c);
  50. }
复制代码


我的 mingw 环境中的 STLport 中的 wcout 就能输出中文了,谁有同类的环境可以试试这个方法。如有问题请帮我指出或修改。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP