ViualStudio 2010 中的 codecvt 结果错误
以下代码在控制台上打印“失败”:
#include "stdafx.h"
#include <locale>
#include <memory>
#include <string>
#include <cstring>
#include <iostream>
int main(int argc, char* argv[])
{
typedef std::codecvt<wchar_t, char, mbstate_t> cvt;
// string to convert
const char cstr[] = "тест";
size_t sz = std::strlen(cstr);
mbstate_t state;
const char *cnext;
wchar_t *wnext;
// buffer to write
wchar_t *buffer = new wchar_t[sz + 1];
std::uninitialized_fill(buffer, buffer + sz + 1, 0);
// converting char* to wchar*, using locale
cvt::result res;
std::locale l(std::locale("Russian_Russia.1251"));
res = std::use_facet<cvt>(l).in(state,
cstr, cstr + sz, cnext,
buffer, buffer + sz + 1, wnext);
if(res == cvt::error)
std::wcout << L"failed" << std::endl;
else
std::wcout << buffer << std::wcout;
delete [] buffer;
return 0;
}
我查看了源代码,发现函数 in() 失败,因为函数 _Mbrtowc (wmbtowc.c) 返回 -1:
if (___mb_cur_max_l_func(locale) <= 1 ||
(MultiByteToWideChar(codepage,
MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
(char *)pst,
2,
pwc,
(pwc) ? 1 : 0) == 0))
{ /* translation failed */
*pst = 0;
errno = EILSEQ;
return -1;
}
因为 ___mb_cur_max_l_func()< /strong> (initctyp.c) 对于 Russian_Russa.1251 语言环境返回 1。这意味着什么?我认为这是不正常的,codecvt 无法将 char* 转换为 wchar_t*。
The following code prints "failed" to console:
#include "stdafx.h"
#include <locale>
#include <memory>
#include <string>
#include <cstring>
#include <iostream>
int main(int argc, char* argv[])
{
typedef std::codecvt<wchar_t, char, mbstate_t> cvt;
// string to convert
const char cstr[] = "тест";
size_t sz = std::strlen(cstr);
mbstate_t state;
const char *cnext;
wchar_t *wnext;
// buffer to write
wchar_t *buffer = new wchar_t[sz + 1];
std::uninitialized_fill(buffer, buffer + sz + 1, 0);
// converting char* to wchar*, using locale
cvt::result res;
std::locale l(std::locale("Russian_Russia.1251"));
res = std::use_facet<cvt>(l).in(state,
cstr, cstr + sz, cnext,
buffer, buffer + sz + 1, wnext);
if(res == cvt::error)
std::wcout << L"failed" << std::endl;
else
std::wcout << buffer << std::wcout;
delete [] buffer;
return 0;
}
I looked into sources and found out that function in() fails because function _Mbrtowc (wmbtowc.c) returns -1:
if (___mb_cur_max_l_func(locale) <= 1 ||
(MultiByteToWideChar(codepage,
MB_PRECOMPOSED|MB_ERR_INVALID_CHARS,
(char *)pst,
2,
pwc,
(pwc) ? 1 : 0) == 0))
{ /* translation failed */
*pst = 0;
errno = EILSEQ;
return -1;
}
because ___mb_cur_max_l_func() (initctyp.c) returns 1 for Russian_Russa.1251 locale. What it means? I think it is not normal, that codecvt can't convert char* into wchar_t*.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你忘了初始化它。使固定:
You forgot to initialize that. Fix: