cd assignment1i.e., a command to change the current working directory to
assignment1. The above command actually consists of two components:
cd-- for change directory
assignment1--the new current working directory
lsin Unix lists the contents of the current directory (same as the
dircommand in Windows). This command takes no arguments.
cp oldfile newfilein Unix copies
newfile. This program takes 2 arguments.
(As an aside, similarly, the program returns to the OS in much the same way, as can be seen from the
return statement in
main. The return value is information passed back by the program to
the OS about the success/failure of the program, and is called the program exit status.)
Thus, we see that a program can be invoked with arguments that provide information to the program from the user. We call these command line arguments.
In order for this information to be of any value to the program, we must be able to access the command line arguments from within the program.
char *) by the OS, placed into an array of such strings (i.e.,
char *or, equivalently,
char **) and passed together with the number of elements in the array as arguments to
main, just as arguments would be passed to any other function.
Thus, the proper signature to
int main(int argc, char **argv)or, equivalently,
int main(int argc, char *argv)This is similar to ANY function that accepts an array (and its corresponding size) as a parameter, except for the fact that whereas we usually place the array first and the number of elements second, it is reversed here (which if you think about it may be the sensible way-- after all, first you have to know the number of elements, and then you can process the elements).
Note: By convention, the arguments are named
vector -- or array), though of course you could
name them anything you want (as long as you maintain the order of count first, then array).
argvis: a 1-dimensional array of C-style character strings. The 0'th element-- which is ALWAYS present-- is the name of the program as invoked on the command line, arguments 1 ...
argc-1are the individual command-line arguments.
finderis a program that accepts a string (the program calls it the pattern and an arbitrary number of file names and prints out the names of all those files that contain the string.
The program accepts an arbitrary number of arguments -- the first (
argv don't forget, the zeroth is the
name of the program) is the pattern, and the rest are the names of the files to be searched.
The program also contains the standard logic for detecting and notifying the user if there are an invalid number of arguments, as well as logic for returning an appropriate exit status: 0 if the pattern was found, 1 if it wasn't found in any of the files, and 2 if there were an invalid # of arguments.
argvargument vector contains C-style strings (
char *) rather than C++-strings (i.e.,
stringclass). You can easily get C++-string from C-style strings by using the
char *in a
char *s; ..... string str = string(s); string str2 = s; string str3(s);(all of the above are just different ways of invoking the constructor that accepts a C-style string).
Furthermore, C-style strings are often implicitly converted to
string for you when necessary.
string can often be used in contexts calling for
However, there is one context that proves confusing and even somewhat mysterious-- when you open
an ifstream or
ofstream, you cannot specify the file name as a
but rather must use a C-style (
char *) string. This is annoying if you want to manipulate
the file name (for example adding or stripping extensions) and are thus working with
objects. However, you can always get a C-style string from a
string object using the