/* * cat.c * * This program accepts filename on the command line and prints the * conents of the file on the standard output */ #include int main (int argc, char *argv[]) { FILE *f; /* the file pointer */ char ch; /* a character to read in */ /* if wrong number of arguments, complain */ if (argc != 2) { fprintf (stderr, "error: wrong number of args\n"); exit (1); } /* try to open the file named in the first command line arg */ f = fopen (argv[1], "r"); if (!f) { /* oops, f was 0, something failed, let's exit */ fprintf (stderr, "error opening file.\n"); exit (1); } /* get a character from the file */ ch = fgetc (f); /* while not at the end of file... */ while (!feof (f)) { /* ... print the current character */ putchar (ch); /* get another character from the file */ ch = fgetc (f); } /* close the file */ fclose (f); return 0; }