Question

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*.

Was it helpful?

Solution

 mbstate_t state;

You forgot to initialize that. Fix:

 mbstate_t state = { 0 };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top