ViualStudio 2010 中的 codecvt 结果错误

发布于 2024-10-12 18:38:11 字数 1791 浏览 7 评论 0原文

以下代码在控制台上打印“失败”:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎盛开的樱花 2024-10-19 18:38:11
 mbstate_t state;

你忘了初始化它。使固定:

 mbstate_t state = { 0 };
 mbstate_t state;

You forgot to initialize that. Fix:

 mbstate_t state = { 0 };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文