- 论坛徽章:
- 24
|
问题基本解决,看来我的判断是对的。
gdb 真是好工具,没有 gdb 我真没办法了。 经过 gdb 跟踪,问题锁定在 STLport 中 的 _fstream.c 文件中的
- template <class _CharT, class _Traits> __BF_int_type__ basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
复制代码 和 codecvt.cpp 的
- codecvt<wchar_t, char, mbstate_t>::result
- codecvt<wchar_t, char, mbstate_t>::do_out(state_type& /* state */,
- const intern_type* from,
- const intern_type* from_end,
- const intern_type*& from_next,
- extern_type* to,
- extern_type* to_limit,
- extern_type*& to_next) const
复制代码
函数中。
现在我把 codecvt<wchar_t, char, mbstate_t>::do_out 改为
- codecvt<wchar_t, char, mbstate_t>::result
- codecvt<wchar_t, char, mbstate_t>::do_out(state_type& /* state */,
- const intern_type* from,
- const intern_type* from_end,
- const intern_type*& from_next,
- extern_type* to,
- extern_type* to_limit,
- extern_type*& to_next) const {
- ptrdiff_t len = (min) (from_end - from, to_limit - to);
- int len_str;
- wchar_t *pwch = (wchar_t*)(from+len);
- *pwch = L'\0';
- //pwch = (wchar_t*)from;
- pwch = (wchar_t*)from;
-
- len_str = wcstombs(NULL, pwch, 0);
- //printf("len_str = %d\n", len_str);
- //len = len_str;
- wcstombs(to, from, len_str);
- *(to+len_str)='\0';
-
- from_next = from + len;
- to_next = to + len_str;
-
- return ok;
- }
复制代码
把 _fstream.c 文件中的
- template <class _CharT, class _Traits> __BF_int_type__ basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
复制代码
改为
- template <class _CharT, class _Traits>
- __BF_int_type__
- basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
- // Switch to output mode, if necessary.
- if (!_M_in_output_mode)
- if (!_M_switch_to_output_mode())
- return traits_type::eof();
- _CharT* __ibegin = this->_M_int_buf;
- _CharT* __iend = this->pptr();
- this->setp(_M_int_buf, _M_int_buf_EOS - 1);
- // Put __c at the end of the internal buffer.
- if (!traits_type::eq_int_type(__c, traits_type::eof()))
- *__iend++ = _Traits::to_char_type(__c);
- // For variable-width encodings, output may take more than one pass.
- while (__ibegin != __iend) {
- const _CharT* __inext = __ibegin;
- char* __enext = _M_ext_buf;
- typename _Codecvt::result __status
- = _M_codecvt->out(_M_state, __ibegin, __iend, __inext,
- _M_ext_buf, _M_ext_buf_EOS, __enext);
- if (__status == _Codecvt::noconv) {
- return _Noconv_output<_Traits>::_M_doit(this, __ibegin, __iend)
- ? traits_type::not_eof(__c)
- : _M_output_error();
- }
- // For a constant-width encoding we know that the external buffer
- // is large enough, so failure to consume the entire internal buffer
- // or to produce the correct number of external characters, is an error.
- // For a variable-width encoding, however, we require only that we
- // consume at least one internal character
-
- else if (__status != _Codecvt::error &&
- (((__inext == __iend /*__iend = (_CharT*)__inext*/) &&
- (/*__enext - _M_ext_buf == _M_width * (__iend - __ibegin)*/ true)) ||
- (!_M_constant_width && __inext != __ibegin)))
-
- {
- // We successfully converted part or all of the internal buffer.
- ptrdiff_t __n = __enext - _M_ext_buf;
-
- if (_M_write(_M_ext_buf, __n))
- __ibegin += __inext - __ibegin;
- else
- return _M_output_error();
- }
- else
- return _M_output_error();
- }
- return traits_type::not_eof(__c);
- }
复制代码
我的 mingw 环境中的 STLport 中的 wcout 就能输出中文了,谁有同类的环境可以试试这个方法。如有问题请帮我指出或修改。 |
|