views:

67

answers:

4

I'm running C++ program in VS2005, and I've set only one argument in project properties-> debug-> command line args, and it's named profile1.dll for example.

here's a code snippet

cout<<"number of arguments:" << argc<<endl;

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

In the output I get

number of arguments:2
argument 0: c
argument 1: p

don't know why it doesn't print the name of the argument?

A: 

Can you put the prototype of your main function? What you are doing is apparently fine.

Make sure your main's function prototype is something similar to:

int main(int argc, char **argv)

Hope it helps.

Pablo Santa Cruz
`int _tmain(int argc, char* argv[])//_TCHAR*`so by default it was _TCHAR* for the argv[], but if so, it print's an integer, so i changed it to char*
kobac
@Kobac: change it back to TCHAR, and use std::wcout instead of std::cout
Sjoerd
Now I see what's happening. You are using UNICODE and 0 are preventing you from printing the whole String. Check Bob's answer out.
Pablo Santa Cruz
+5  A: 

Does the name of your exe start with C? If you expect a string and you only get one character, it's usually because you've discovered that the Western alphabet in UTF-16 Unicode effectively puts a 0 between alternating ANSI chars. Are you compiling for Unicode ?

Bob Moore
I'm using Unicode.I changed it to multi-byte, but still the same output
kobac
@Kobac: When using Unicode, you should use std::wcout instead of std::cout.
Sjoerd
+1 - good catch!
SB
Unicode != UTF16 ! Use wcout with wstring.
anno
+3  A: 

argv[0] is the name of your program. argv[1] is the first parameter. It sounds like you have declared the relevant parameter in main() as char* argv rather than char *argv[] or char **argv.

Vicky
But the OP already said in response to Pablo's answer that he had declared it as char* argv[]. Something's not right here.
Bob Moore
@kobac: thanks for accepting my answer, but as pointed out it wasn't the cause of your problem. Can you accept @Bob Moore's answer instead?
Vicky
fairplay of the year! :)
kobac
+1  A: 

Leave TCHAR be, it's fine.

If you compile with unicode, use wcout for output:

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

  for (int i=0; i<argc; i++)
    wcout << "argument " << i << ": " << argv[i] << endl;

    return 0;
}

Otherwise compile without unicode and your code will just work as-is (and will not work with unicode parameters :o)

You can find setting in "Project Properties/General/Character Set"

MaR