Argc & Argv
Command Line Arguments
- When we have run programs up until now, we have only run the program name on the command line
- You can write a program to allow the user to write more commands besides the program name & use that information in the program as you wish.
- In the example below, we have the program name (lab5) and arguments following, separated by spaces
1
|
./lab5 data1 data2 10 20 30
|
This data is provided to int main() right when lab5 is executed.
Main Parameters
- You give main() 2 parameters in C++ to have command line arguments. These are the only two arguments main() is allowed to have.
- Argc (an integer)
- Argv (an array of c-style strings)
- You can write this in two different ways:
-
int main(int argc, char **argv)
OR
int main(int argc, char *argv[])
- The only difference is that we have two ways to declare a c-style string. Both are valid. I tend to use the first way as it’s faster to type.
1
2
3
|
int main(int argc, char *argv[]) // This is all that is needed to have command line arguments.
{
}
|
-
argc
- The number of arguments in the argv vector (vector means array in this case).
-
argv[]
- an array of char * (char * is a C-style string).
1
|
int main(int argc, char **argv)
|
- Again,
argc
is the count of arguments (i.e. number of commands typed on the command line when the user runs the program).
- element
argv[i]
will be a c-style string containing the i-th command line argument
- Therefore,
argv
is defined from 0 through argc-1.
Argc
1
|
int main(int argc, char **argv)
|
- If
argc
is equal to 2, then you have one argument to your program (because your program name is the first argument on the command line)
- You can use
argc
to check if the user is running your program with the correct number of arguments
Argv
1
|
int main(int argc, char **argv)
|
- Whenever you use
argv
, unless you are just printing out the strings, you should convert them to C++ strings or use stringstreams to change them to other data types (ints, doubles, etc).
string a1, a2;
a1 = argv[1];
a2 = argv[2];
Example Usage
1
2
3
4
5
6
7
8
|
int main(int argc, char *argv[])
{
for (int i = 0; i < argc; i++) {
cout << "Argument " << i << " = "
<< argv[i] << '\n';
}
return 0;
}
|
- ALL arguments are C-style strings.
- Use an istringstream to convert arguments to other data types.
1
2
|
./args
Argument 0 = ./args
|
1
2
3
4
5
|
./args 10 20 30
Argument 0 = ./args
Argument 1 = 10
Argument 2 = 20
Argument 3 = 30
|
1
2
3
4
5
|
./args hello 20.2 30
Argument 0 = ./args
Argument 1 = hello
Argument 2 = 20.2
Argument 3 = 30
|
Convert Args with String Streams
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//.args file.txt number_of_lines_to_read
//argv[0] argv[1] argv[2]
int main(int argc, char *argv[])
{
if (argc < 3) {
cout << "Usage: " << argv[0] << " filename lines_to_read\n";
return -1;
}
istringstream sin(argv[2]); //Need # of lines to be an int
string filename(argv[1]); //Automatic conversion from C to C++
int lines;
if (!(sin >> lines)) { //Always check to see if extraction failed!
cout << "Error in extracting number of lines.\n";
return -2;
}
}
|
We can keep the first argument (the file name) as a string and can easily automatically convert it from a C to C++ string, but we want the number of lines to read stored as an integer. In order to change the C-style string argument to an integer, we can use stringstreams, specifically an istringstream (string is input).
Notice we used different return values for main to denote different errors, in case the messaging wasn’t enough. You could also return 1 as we typically do when we exit the program prematurely due to a user error.
Error Checking
- It is good practice to check
argc
at the beginning of the program to verify the user ran the program with the correct number of arguments.
- Always check
argc
before using argv
.
Powerpoint