/* Name: readfile.c Purpose: Read and store lines from an input file (w/ dynamic allocation) Author: Bill Slough */ #include #include #include "util.h" #define FILE_NAME "words" /* name of the input file */ int main(void) { FILE *fp; /* file pointer for an input stream */ char **lines; /* NULL-terminated array of lines */ int i; /* 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); } /* read and store all lines from the input file */ lines = read_lines(fp); /* report the number of lines read and saved */ display_lines(lines); display_lines_in_reverse(lines); printf("Number of lines in the file = %d\n", count_lines(lines)); /* Free the space used for the lines */ i = 0; while (lines[i] != NULL) { free(lines[i]); i++; } free(lines); /* Close the stream */ fclose(fp); return 0; }