382 Chapter 17 Miscellaneous and Advanced Features
If argcis equal to 2, the lookupfunction is called to find the word pointed to by
argv[1]in the dictionary. If the word is found, its definition is displayed.
As another example of command-line arguments, Program 16.3 was a file-copy pro-
gram. Program 17.1, which follows, takes the two filenames from the command line,
rather than prompting the user to type them in.Program 17.1 File Copy Program Using Command-Line Arguments
// Program to copy one file to another -- version 2#include <stdio.h>int main (int argc, char *argv[])
{
FILE *in, *out;
int c;if ( argc != 3 ) {
fprintf (stderr, "Need two files names\n");
return 1;
}if ( (in = fopen (argv[1], "r")) == NULL ) {
fprintf (stderr, "Can't read %s.\n", argv[1]);
return 2;
}if ( (out = fopen (argv[2], "w")) == NULL ) {
fprintf (stderr, "Can't write %s.\n", argv[2]);
return 3;
}while ( (c = getc (in)) != EOF )
putc (c, out);printf ("File has been copied.\n");fclose (in);
fclose (out);return 0;
}The program first checks to make certain that two arguments were typed after the pro-
gram name. If so, the name of the input file is pointed to by argv[1], and the name of