Search code examples
c++argvwchar-twidestring

What is the issue in following conversion from argv[1] to char * string?


I am very new to C and pointers. I am trying to convert command line argument to wchar_t * . But somehow it is not giving proper output. What am I missing?

void fun(){
    std::setlocale(LC_ALL, "en_US.utf8");
    std::wcout.imbue(std::locale("en_US.utf8"));
    char* mbstr = "f:\\mypath1\\mypath2\\mypath3";
    wstring reposPath;
    char *c_ReposPathString = (char*)mbstr;
    size_t c_ReposPathStringSize= 0;
    if(c_ReposPathString)   
    {       
         c_ReposPathStringSize = 2*(strlen(c_ReposPathString)+1);   
    }
    wchar_t *w_ReposPathChar = new wchar_t[c_ReposPathStringSize];  
    if(w_ReposPathChar) 
    {       
       mbstowcs(w_ReposPathChar, c_ReposPathString, c_ReposPathStringSize);
    }
       reposPath = w_ReposPathChar;

    printf("%s",  (char *)reposPath.c_str());
    free(w_ReposPathChar);
}

when I print length of w_path, it shows 1. But argv[1] has more than one character it it.


Solution

  • You can't simply re-cast a wchar_t string to a char string and expect it to work, as there may (will) be many wchar_t values that have their upper byte as zero (which will be seen as a terminator, after the cast).

    So, instead of:

    printf("%s",  (char *)reposPath.c_str());
    

    which sees a 'false' nul-terminator after the f, simply print the wchar_t string for what it is:

    printf("%ws", reposPath.c_str());
    

    Also, you have a const missing in your declaration of mbstr, which should be this:

    const char* mbstr = "f:\\mypath1\\mypath2\\mypath3";
    

    and you don't need to allocate twice the number of char for your wchar_t buffer, so this will suffice:

        if (c_ReposPathString)
        {
            c_ReposPathStringSize = strlen(c_ReposPathString) + 1; // Don't need "* 2"
        }
    

    Feel free to ask for further clarification and/or explanation.