/* * Test readLine * * First design decision: Interactive or non-interactive? * Suppose non-interactive. "Give it a file". * Second design decision: How? * Use #define to make it a constant. * Pass it as an argument to main. * Hard-code it. * Somewhat interactive: prompt for it. */ #include #include #include "readLine.h" #define FILE_TO_READ "readLine.h" #define MAX 6 int peek(FILE *infile) { int ch; ch = getc(infile); ungetc(ch, infile); return ch; } main(int argc, char *argv[]) { FILE *infile; char str[MAX]; if (argc != 2) { fprintf(stderr, "Usage: testrl filename\n"); exit(EXIT_FAILURE); } infile = fopen(argv[1], "r"); if (infile == NULL) { fprintf(stderr, "File %s does not exist\n", argv[1]); fprintf(stderr, "Usage: testrl filename\n"); exit(EXIT_FAILURE); } /* Read everything from the file and print it to the screen. */ /* Stop when you hit EOF. */ /* while (!feof(infile)) { */ while (peek(infile) != EOF) { if (readLine(infile, str, MAX)) printf("Full line: "); else printf("Partial line: "); printf("'%s'\n", str); } /* while */ fclose(infile); exit(EXIT_SUCCESS); } /* main() */