[Command-Line Arguments]
[Christopher Bartholomew - Harvard University]
[This is CS50 - CS50.TV]
A useful feature for a program is to accept user input.
Thus far, we've explored some functions within the CS50 library
to accept user input, such as "get string,"
which prompts the user, while the application is running, for a string.
>> However, there are cases where you want to provide your program input
before it is actually running.
This way, you don't need to ask additional information from your user
while executing a simple task.
Take, for example, the mv or move command in UNIX.
This command allows the user to move a file from one location to another.
According to the manual pages, mv accepts two command line arguments:
the file that is being moved and the location the file is being moved to.
So this example has a command with two arguments.
So how do we tell our C program to utilize these command-line arguments?
>> Well, it turns out that main, which we use in all C programs, has a secret.
Main accepts two parameters: argc and argv.
Let's go over these terms.
>> The first parameter, argc, which stands for argument count,
has a data type of integer.
The argc parameter contains the number of arguments, including the command.
In our move command, although we only have two arguments displayed,
argc's value will be 3.
The second parameter, argv, which stands for argument vector,
is an array of char pointers that point to strings.
>> This means that each element in argv, starting from zero,
contains the command and arguments.
For example, argv[0], which I'll refer to as argv zero,
will always contain the command that is being run--
in this case, mv.
argv[1] will contain the first argument, file.txt,
and argv[2] will contain the second argument, ~/cs50/.
The last argument of argv will always be null.
So let's implement these command-line arguments.
In previous exercises, we placed void, meaning nothing, as main's parameter.
However, in order for us to use command-line arguments,
we need to remove void and place inside of main int argc, char* argv[].
Now, to access the entire element from argv, which are your arguments,
you can simply iterate, or loop, through the array like this.
So, inside of main's body, we're going to go ahead and type a for loop:
for (int i = 0; i < argc; i++).
>> We don't need a curly brace here because we're only executing one line of code
within the body of this loop.
We'll go ahead and hit tab once,
then type printf("argv[%d], to represent an integer value,
is %s, for string, then the new line character.
Then we provide printf i for the current iteration of the loop
and argv[i] for the string representation of the current command-line argument.
When we run it with two arguments, we'll see the arguments being displayed in the terminal.
Earlier we said that the argv held an array of char pointers.
>> So, if this is the case, how do we then access individual characters in each argument?
For example, what if I wanted to look for a specific character in the first argument?
Well, the answer is that we need to apply a nested loop
that will then iterate through each of the elements in the argument string.
This is how you do it.
>> First, we're going to make a copy of example2.c.
Then, inside of the first for loop,
we're going to add an additional for loop.
So for (int j = 0, n = strlen(argv[i]),
which then gives us the length of the current argument,
; j < n; j++)
We're going to print the location of each character
inside of the current argument by using printf.
So, printf("argv[%d], to represent the index of the current argument,
then [%d] once again, to represent the current character of the current argument,
is: %c, for the current character in the argument.
Lastly, we provide printf with the index of the outer loop, i,
then the index of the inner loop.
>> And our last argument to printf is the actual character from the argument provided
at the command line.
Now, because I used the string function strlen to obtain the length of a string,
I must also add the string.h library to the top of our includes.
So, to do that, we'll go up, and just under stdio.h, we're going to do
#include .
>> So, let's compile and run and provide it an actual argument.
>> And, as we can see, we now have the exact location of each individual char in the argument.
So that's it. I'm Christopher Bartholomew; this is CS50.
[CS50.TV]