/* Name: readfile1.c Purpose: Read and display character data from the text file named "words" Author: Bill Slough */ #include #include #define FILE_NAME "words" /* name of the input file */ int main(void) { FILE *fp; /* file pointer for an input stream */ int ch; /* a character from this stream */ int charCount; /* number of characters processed */ int lineCount; /* number of lines processed */ /* Try to open the input stream for reading */ fp = fopen(FILE_NAME, "r"); if (fp == NULL) { fprintf(stderr, "Error: can't open %s for reading.\n", FILE_NAME); exit(EXIT_FAILURE); } /* process the file, character by character */ charCount = 0; lineCount = 0; while ((ch = getc(fp)) != EOF) { putchar(ch); charCount++; if (ch == '\n') lineCount++; } printf("Characters processed = %d\n", charCount); printf("Lines processed = %d\n", lineCount); /* close the stream */ fclose(fp); return 0; }