// Simple implementation of reading files // passed as command line arguments // using fgetc - in other words cat // Andrew Taylor - andrewt@unsw.edu.au // 08/3/19 #include int main(int argc, char *argv[]) { // go through a list of files specified on the command line // printing their contents in turn to standard output int argument = 1; while (argument < argc) { // FILE is an opaque (hidden type) defined in stdio.h // "r" indicate we are opening file to read contents FILE *stream = fopen(argv[argument], "r"); if (stream == NULL) { perror(argv[argument]); // prints why the open failed return 1; } // fgetc returns next the next byte (ascii code if its a text file) // from a stream // it returns the special value EOF if not more bytes can be read from the stream int c = fgetc(stream); // return bytes from the stream (file) one at a time while (c != EOF) { fputc(c, stdout); // write the byte to standard output c = fgetc(stream); } fclose(stream); argument = argument + 1; } return 0; }