Computing.Net > Forums > Programming > C++ Command-Line Arguments

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ Command-Line Arguments

Reply to Message Icon

Name: muska
Date: December 17, 2002 at 06:20:26 Pacific
OS: N/A
CPU/Ram: N/A
Comment:

When you want to retreive command line arguments sent to a program in C++, you say

int main(int argc, **char argv)
{
// code here
}

What I am wondering is why is there a variable that holds the number of arguments sent to the program when that value can be found with argv.length?

Second (forgive me, I am still learning) what does **char variableName mean?



Sponsored Link
Ads by Google

Response Number 1
Name: Ronin1
Date: December 17, 2002 at 09:40:49 Pacific
Reply:

argc holds your command line entries whereas argv[] hold what is in those entries, so the length of the string in argv[] isn't what you would want. As an example

myprogram.exe

argc = 1
argv[0] = myprogram.exe

myprogram.exe somefile.ext /s

argc = 3
argv[0] = myprogram.exe
argv[1] = somefile.txt
argv[2] = /s

The practical use depends on your program

if(argc < 3)
{
cout [[ "syntax: myprogram.exe somefile.ext /option";
return -1;
}

**char is a pointer to the pointer argv[] so that you don't use the array form argv[]. I *think* but I don't use that form.

Try this with a few different command line entries.

int i;

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


0

Response Number 2
Name: muska
Date: December 17, 2002 at 11:44:53 Pacific
Reply:

Thanks for the reply.

Could you not say
for(i = 0; i < argv.length; i++)
?

I still don't see what the difference is between argc and argv.length.


0

Response Number 3
Name: Don Arnett
Date: December 17, 2002 at 12:26:46 Pacific
Reply:

Unless something has changed since I learned C++, argv.length doesn't exist. argv is of type "char **", it is not an object. Have you tried to use argv.length in a program?? I'll bet that it won't compile.


0

Response Number 4
Name: Ronin1
Date: December 17, 2002 at 15:19:35 Pacific
Reply:

I dunno if argv.length exists or not (maybe it does after the ansi 99 updates.) I'd just assume use strlen and strcpy for that sort of thing.

I don't suspect that argv.length would contain how many entries there were, but how many characters are in that entry in the way that it looks like you are trying to use it.

If you don't mind, post an example of how you're using argv.length


0

Response Number 5
Name: muska
Date: December 17, 2002 at 16:26:31 Pacific
Reply:

I now see that the length() method is a part of a class called "apstring" which I used in my computer science class. I also see that this does not work with arrays.

Nevermind, you guys were right. I didn't realize that this was the case. I see why they have that variable.

Also, it was char **argv instead of **char argv.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ Command-Line Arguments

command line argument in C ?? www.computing.net/answers/programming/command-line-argument-in-c-/4446.html

c++ command line editing www.computing.net/answers/programming/c-command-line-editing/7730.html

Command line arguments in VisualC++ www.computing.net/answers/programming/command-line-arguments-in-visualc/6012.html