reading from the command line

I want to create a program that reads the name of the file after the the command line.

For instance the name of my program is main.cpp

The user executes my program by typing:

./main input1.txt

I want main to read the string "input1.txt". How do I do that?
main() can have one of two forms:

int main(void)
and
1
2
int main(int argc, char**argv)
//you can name them whatever you wish, though 


Use the second version. argc is the number of arguments passed to the program, and argv is the array of cstrings of the arguments. e.g.


Someone types: ./myprog arg1 arg2

argc = 3
argv[0] = "./myprog"
argv[1] = "arg1"
argv[2] = "arg2"
Thank you so much!
Do you mean
 
int main (int argc, char *argv[])

?
Last edited on
An array is a specialized pointer, essentially, so *argv[] is (roughly) the same as **argv as far as arguments are concerned.
Yes, that's what he meant. Use the *argv[] form. It is canonical.

thanks
Topic archived. No new replies allowed.