Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, command line args not being parsed properly

I have a program:

int _tmain(int argc, char* argv[])
{
   std::cout << "STARTING" << std::endl;
   std::cout << "Num inputs: " << argc << std::endl;

   for(int i = 0; i < argc; i++)
      std::cout << argv[i] << std::endl;

that I expect to print out all the command line arguments. However, the output is this:

./Test.exe hello world

STARTING Num inputs: 3 . h w

It appears that it's only looking at the first char in each argument and not the entire char* till termination character.

Anyone have any thoughts?

Additional Notes: Creating it through VS2008, and I am essentially copying and pasting an example on the internet that should work. I have run the program in bash, powershell, and cmd.

like image 418
jbu Avatar asked Dec 07 '10 21:12

jbu


1 Answers

Your Visual C++ project is set to Unicode, and your main function is called _tmain. This means that Windows invokes your function and passes you Unicode strings, but you're treating them as ANSI strings by using the char* type. Since the second byte of the first Unicode character is null, this appears as an ANSI string with one character.

like image 81
Dark Falcon Avatar answered Sep 26 '22 00:09

Dark Falcon