C language programming, often encountered in the main function argc and argv[] these two parameters. ARGC is the abbreviation for argument count, which is the number of parameters, argv is the abbreviation of argument vector, which is the parameter list. Argv[0] is the name of the program itself, Argv[1] is the parameter of the first program entered on the command line, ARGV[ARGC] is null, as follows:
#include "stdio.h" int main (int argc, char *argv[]) { printf ("The argc value is%d \ n", argc); int i; for (i = 0; I <= argc; i++) { printf ("The argv[%d] value is%s \ n", I, Argv[i]); } return 0;} #将上述代码编译为test可执行文件, enter the following on the command line/*./test arg_1 arg_2*/#执行结果如下:/*the argc value is 3 the argv[0] value is./test_c_0 the argv[ 1] value is arg_1 the argv[2] value was arg_2 the argv[3] value is (NULL) */
Figuring out argc and argv[], we can use both to transfer the file name parameters that will be processed by the command line to the program, as shown in the code below.
#include "stdio.h" int main (int argc, char *argv[]) { FILE *fp; int C; fp = fopen (Argv[1], "R"); while ((c = fgetc (FP))! = EOF) { printf ("%c", c); } Fclose (FP); return 0;}
C-Enter the name of the file at the command line and print the contents of the file