/* --- Computer Science, Monash University, Australia 3168, 16/4/1999 --- */ #include main(int argc, /* Count of command line arguments (parameters) */ char *argv[] /* Values of parameters: array of string (pointer to char) */ ) { int i; FILE *inp; int ch; printf("command line:"); for(i = 0; i < argc; i++) printf(" argv[%d]=\"%s\"", i, argv[i]); /* print each cmd line param */ printf("\n"); /* NB argv[0] = program name */ inp = stdin; /* default input file, but...*/ /* if there is a genuine */ if(argc > 1) /* command line parameter... */ { inp = fopen(argv[1], "r"); /* mode "r" i.e. read, text */ if(!inp) /* error, could not open it */ { printf("could not open file \"%s\", halting\n", argv[1]); exit(1); } } else /* no genuine command line parameters, use stdin */ printf("no file specified, will echo what you type, end with ^D\n"); i = 1; /* prove we have the right file */ printf("%3d: ", 1); while((ch = fgetc(inp)) != EOF) /* read each character in inp */ { printf("%c", ch); /* and print it */ if(ch == '\n') printf("%3d: ", ++i); /* number the lines */ } }/*main*/ /* Command-Line Parameters & Opening a Named File, L.Allison Comp Sci Monash */