1

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.

  • Can you edit your code into something that we can compile and test: like where is `pathSize` defined, what is `null` (did you mean `NULL` or `nullptr`) and how you are getting the length of `w_path`? – Adrian Mole Jan 09 '20 at 06:28
  • … because when I add the (guessed) assumptions, your code gives me the expected answers. – Adrian Mole Jan 09 '20 at 06:32
  • I have added a function here. I don't want to use %S to print wstring. so Basically I want to convert argv[1] value to dynamically created wstring. Assuming that path will be coming from argv[1] – mathewFarrel Jan 09 '20 at 06:46
  • `char` is usually 1 byte, whilst `wchar_t` is 2 bytes. – Kagiso Marvin Molekwa Jan 09 '20 at 07:04

1 Answers1

0

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83