Click to See Complete Forum and Search --> : How to pass arguments in C++
Gaccm
07-25-2001, 05:51 PM
The title says it all. I want to be able to run a program in a script, and thus the data must be passed in a "./runme datahere" format since the prog cant be interactive.
Is this possible? is it possible using non-standard libraries?
Stuka
07-25-2001, 05:59 PM
Declare your main function like so:int main(int argc, char* argv[]){
}argc will contain the number of command line arguments, and argv will be an array of char* containing these arguments - argv[0] is always the program name (./runme in your example), then the arguments follow from there (datahere would be argv[1] in your example).
<edit>Left out a comma</edit>
[ 25 July 2001: Message edited by: Stuka ]
Strike
07-25-2001, 06:04 PM
If you want a few command line args, look into using getopt(3).
Gaccm
07-25-2001, 06:56 PM
wow.... thats so obvious, well pretty obvious. Anyways, i have another obvious question! i'm wondering, how would i let the input be numbers? (/runme 1 2 3) i thought replacing the char wit hvector would work, but i guess this system doesn't know what a 'vector' is. Anyone know another type of variable for a string of numbers? i guess that get thing might work.
it's the same way whether numbers or strings... arg[0] will be the program name, then in your example arg[1] will be the string "1"... then just cast it to an integer and so on
-SiM
tecknophreak
07-26-2001, 08:29 AM
Should it be
int main(int argc, char* argv[] {
}
or
int main(int argc, char **argv) {
}
:rolleyes:
I know most don't care, but I learned the second way and I'm falling a little behind on the standard. I learned 2 years ago and so many new things have happened.
The shame is, my job programming is in C :mad: so I don't get any good practice.
Stuka
07-26-2001, 12:06 PM
technophreak-
Those two things mean the same thing. I personally like to think of it as char*[], but either way works. I'm not sure what any particular standards body says, or if they say anything - as far as I'm concerned, it's a stylistic difference.
Gaccm-
The main function has to be declared with (int, char*[]). You can't change this. As was pointed out, just convert the string "1" to the int 1 - atoi() should work nicely for you.
tecknophreak
07-26-2001, 01:08 PM
Stuka - you can call me teck, its shorter.
I was just wondering about the ** vs the *[] as to which one was being pushed more now. I had a prof who was all sorts of into the standard of the day. And of course then it rubbed off on me. There's only a few things I'm anal about code and math proofs. (I had an anal prof in math too)
Oh and I know that they're the same, just to clarify, sorry for not doing so earlier.
TheLinuxDuck
07-26-2001, 03:00 PM
Originally posted by tecknophreak:
<STRONG>I was just wondering about the ** vs the *[] as to which one was being pushed more now.</STRONG>
Teck, I'm with you.. I've always done it as:
int main(int argc, char **argv)
It looks weird to me the other way. (^=
tecknophreak
07-26-2001, 03:19 PM
Thanks Duck! The *argv[] way seems odd, like there's an evolution going on so that in a few months or weeks or something(computers go fast) we'll have *gasp* argv[][] :eek: . But I suppose it is still just preference.
TheLinuxDuck
07-26-2001, 03:34 PM
Unless I'm just really mistaken, I think that kind of thing is more picky when dealing with variables inside of the program. It's been a while since I've done any two-dimensional arrays like that, but it seems like the last time I did, the inner-most dimensions had to be defined exactly as they were created, such that:
int myInt[10][20];
:could defined in a function param list as:
void functionOne(int *tempInt[10]);
// or
void functionTwo(int tempInt[10][]);
I could be wrong, but it seems like I *had* to specify the size of the inner most dimension(s).
It's been as while, as I said, so I could be digging from my nose on this one. (^=