Question

I'm writing a C++ app for windows console and it get parameters and "casts" them as a char*[]. meaning I changed this:

int _tmain(int argc, _TCHAR* argv[])

to this:

int _tmain(int argc, char* argv[])

Now when I parse it, I can't seem to find the end of the each parameter. If I get argv[1] it just returns the dash ('-'). I tried looping through the characters using sizeof like this:

    for(j=0; j<sizeof(argsIn[i]); j++)
    {
        std::cout<<argsIn[i][j];
    }

the output here will put spaces between each character and output exactly 4 character total each time co if the command was >cmd.exe -d frank this would output f r when i=3. I also tried checking for the null charcter like this:

    while(argsIn[i][j] != '\0')
    {
        std::cout<<argsIn[i][j];
        j++;
    }

this method only outputs the first character. So in the above example, when i=3 we would just get f Any help would be appreciated.

Was it helpful?

Solution

It sounds like you are trying to interpret UTF-16 characters as ASCII. Are you compiling your code as Unicode?

If you need your parameters as ASCII, either compile your code as ASCII, or keep _TCHAR* and use WideCharToMultiByte() to convert the arguments.

OTHER TIPS

Because your program is Unicode. You should change the main syntax to:

int _tmain(int argc, wchar_t* argv[])

In Unicode each character has two bytes. For English character second byte is always NULL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top