C examples for Function:main function
Write a program that sequentially displays on screen all the files listed in the command line.
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE * fp;//from w w w . ja va 2s. c om int ch; if (argc == 1) { printf("Usage: %s file1 file2 ...\n", argv[0]); exit(EXIT_FAILURE); } for (int i = 1; i < argc; i++) { if ((fp = fopen(argv[i], "r")) == NULL) { fprintf(stderr, "Could not open file %s.\n", argv[i]); exit(EXIT_FAILURE); } while ((ch = getc(fp)) != EOF) putc(ch, stdout); fclose(fp); } return 0; }