版本1和2间的区别
于2006-06-18 18:00:31修订的的版本1
大小: 8145
编辑: czk
备注:
于2007-07-18 20:05:48修订的的版本2
大小: 8219
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 1: 行号 1:
## page was renamed from Pointers and Arrays/5.10 Command-line Arguments

Navigation(slides)

5.10 Command-line Arguments

In environments that support C, there is a way to pass command-line arguments or parameters to a program when it begins executing. When main is called, it is called with two arguments. The first (conventionally called argc, for argument count) is the number of command-line arguments the program was invoked with; the second (argv, for argument vector) is a pointer to an array of character strings that contain the arguments, one per string. We customarily use multiple levels of pointers to manipulate these character strings.

The simplest illustration is the program echo, which echoes its command-line arguments on a single line, separated by blanks. That is, the command

   1    echo hello, world

prints the output

   1    hello, world

By convention, argv[0] is the name by which the program was invoked, so argc is at least 1. If argc is 1, there are no command-line arguments after the program name. In the example above, argc is 3, and argv[0], argv[1], and argv[2] are "echo", "hello,", and "world" respectively. The first optional argument is argv[1] and the last is argv[argc-1]; additionally, the standard requires that argv[argc] be a null pointer.

attachment:pic511.gif

The first version of echo treats argv as an array of character pointers:

   1    #include <stdio.h>
   2 
   3    /* echo command-line arguments; 1st version */
   4    main(int argc, char *argv[])
   5    {
   6        int i;
   7 
   8        for (i = 1; i < argc; i++)
   9            printf("%s%s", argv[i], (i < argc-1) ? " " : "");
  10        printf("\n");
  11        return 0;
  12    }

Since argv is a pointer to an array of pointers, we can manipulate the pointer rather than index the array. This next variant is based on incrementing argv, which is a pointer to pointer to char, while argc is counted down:

   1    #include <stdio.h>
   2 
   3    /* echo command-line arguments; 2nd version */
   4    main(int argc, char *argv[])
   5    {
   6        while (--argc > 0)
   7            printf("%s%s", *++argv, (argc > 1) ? " " : "");
   8        printf("\n");
   9        return 0;
  10    }

Since argv is a pointer to the beginning of the array of argument strings, incrementing it by 1 (++argv) makes it point at the original argv[1] instead of argv[0]. Each successive increment moves it along to the next argument; *argv is then the pointer to that argument. At the same time, argc is decremented; when it becomes zero, there are no arguments left to print.

Alternatively, we could write the printf statement as

   1    printf((argc > 1) ? "%s " : "%s", *++argv);

This shows that the format argument of printf can be an expression too.

As a second example, let us make some enhancements to the pattern-finding program from Section 4.1. If you recall, we wired the search pattern deep into the program, an obviously unsatisfactory arrangement. Following the lead of the UNIX program grep, let us enhance the program so the pattern to be matched is specified by the first argument on the command line.

   1    #include <stdio.h>
   2    #include <string.h>
   3    #define MAXLINE 1000
   4 
   5    int getline(char *line, int max);
   6 
   7    /* find:  print lines that match pattern from 1st arg */
   8    main(int argc, char *argv[])
   9    {
  10        char line[MAXLINE];
  11        int found = 0;
  12 
  13        if (argc != 2)
  14            printf("Usage: find pattern\n");
  15        else
  16            while (getline(line, MAXLINE) > 0)
  17                if (strstr(line, argv[1]) != NULL) {
  18                    printf("%s", line);
  19                    found++;
  20                }
  21        return found;
  22    }

The standard library function strstr(s,t) returns a pointer to the first occurrence of the string t in the string s, or NULL if there is none. It is declared in <string.h>.

The model can now be elaborated to illustrate further pointer constructions. Suppose we want to allow two optional arguments. One says print all the lines except those that match the pattern; the second says precede each printed line by its line number.

A common convention for C programs on UNIX systems is that an argument that begins with a minus sign introduces an optional flag or parameter. If we choose -x (for except) to signal the inversion, and -n (number) to request line numbering, then the command

   1    find -x -npattern

will print each line that doesn't match the pattern, preceded by its line number.

Optional arguments should be permitted in any order, and the rest of the program should be independent of the number of arguments that we present. Furthermore, it is convenient for users if option arguments can be combined, as in

   1    find -nx pattern

Here is the program:

   1    #include <stdio.h>
   2    #include <string.h>
   3    #define MAXLINE 1000
   4 
   5    int getline(char *line, int max);
   6 
   7    /* find: print lines that match pattern from 1st arg */
   8    main(int argc, char *argv[])
   9    {
  10        char line[MAXLINE];
  11        long lineno = 0;
  12        int c, except = 0, number = 0, found = 0;
  13 
  14        while (--argc > 0 && (*++argv)[0] == '-')
  15            while (c = *++argv[0])
  16                switch (c) {
  17                case 'x':
  18                    except = 1;
  19                    break;
  20                case 'n':
  21                    number = 1;
  22                    break;
  23                default:
  24                    printf("find: illegal option %c\n", c);
  25                    argc = 0;
  26                    found = -1;
  27                    break;
  28                }
  29        if (argc != 1)
  30            printf("Usage: find -x -n pattern\n");
  31        else
  32            while (getline(line, MAXLINE) > 0) {
  33                lineno++;
  34                if ((strstr(line, *argv) != NULL) != except) {
  35                    if (number)
  36                        printf("%ld:", lineno);
  37                    printf("%s", line);
  38                    found++;
  39                }
  40            }
  41        return found;
  42    }

argc is decremented and argv is incremented before each optional argument. At the end of the loop, if there are no errors, argc tells how many arguments remain unprocessed and argv points to the first of these. Thus argc should be 1 and *argv should point at the pattern. Notice that *++argv is a pointer to an argument string, so (*++argv)[0] is its first character. (An alternate valid form would be **++argv.) Because [] binds tighter than * and ++, the parentheses are necessary; without them the expression would be taken as *++(argv[0]). In fact, that is what we have used in the inner loop, where the task is to walk along a specific argument string. In the inner loop, the expression *++argv[0] increments the pointer argv[0]!

It is rare that one uses pointer expressions more complicated than these; in such cases, breaking them into two or three steps will be more intuitive.

Exercise 5-10. Write the program expr, which evaluates a reverse Polish expression from the command line, where each operator or operand is a separate argument. For example,

   1    expr 2 3 4 + *

evaluates 2 * (3+4).

Exercise 5-11. Modify the program entab and detab (written as exercises in Chapter 1) to accept a list of tab stops as arguments. Use the default tab settings if there are no arguments.

Exercise 5-12. Extend entab and detab to accept the shorthand

   1    entab -m +n

to mean tab stops every n columns, starting at column m. Choose convenient (for the user) default behavior.

Exercise 5-13. Write the program tail, which prints the last n lines of its input. By default, n is set to 10, let us say, but it can be changed by an optional argument so that

   1    tail -n

prints the last n lines. The program should behave rationally no matter how unreasonable the input or the value of n. Write the program so it makes the best use of available storage; lines should be stored as in the sorting program of Section 5.6, not in a two-dimensional array of fixed size.

Navigation(siblings)

TCPL/5.10_Command-line_Arguments (2008-02-23 15:34:07由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.