0

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?

2
  • 1
    My guess is your main method has argv has type char* rather than char**. Can't know for sure without seeing your main method decl. Commented Jul 28, 2010 at 11:39
  • I think Bob's answer is THE one. You are using UNICODE and 0 is preventing you from printing the whole string. Use UNICODE's functions to print the string. Commented Jul 28, 2010 at 11:54

4 Answers 4

5

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 ?

Sign up to request clarification or add additional context in comments.

2 Comments

I'm using Unicode. I changed it to multi-byte, but still the same output
@Kobac: When using Unicode, you should use std::wcout instead of std::cout.
3

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.

2 Comments

But the OP already said in response to Pablo's answer that he had declared it as char* argv[]. Something's not right here.
@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?
1

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"

Comments

0

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.

3 Comments

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: change it back to TCHAR, and use std::wcout instead of std::cout
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.