dragan.stepanovic
dragan.stepanovic

Reputation: 3005

problem with reading program arguments in Visual Studio C++

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?

Upvotes: 0

Views: 1791

Answers (4)

Bob Moore
Bob Moore

Reputation: 6894

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 ?

Upvotes: 5

MaR
MaR

Reputation: 829

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"

Upvotes: 1

Vicky
Vicky

Reputation: 13244

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.

Upvotes: 3

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

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.

Upvotes: 0

Related Questions