#include #include #include #define TRUE 0 #define FALSE 1 #define LINELENGTH 80 char *line; char *word; /* reads a line; returns number of characters read */ int getline (FILE *fp) { int i = 0; char c; c = fgetc(fp); while ((c != EOF) && (c != '\n')) { if (i == LINELENGTH) { fprintf (stderr, "Line too long, aborting!\n"); exit (1); } line[i] = c; i++; c = fgetc(fp); } return i; } /* check line for occurrences of character a */ int checkOccurrence (char *line) { int result = FALSE; /* inspect each character in turn */ while (line) { if (line == 'a') { result = TRUE; } line++; } return result; } int main (int argc, char **argv) { FILE *fp; int count; int charInLine; /* check for correct number of arguments */ if (argc != 2) { fprintf (stderr, "%s: Exactly one argument required\n", argv[0]); exit (1); } argv++; /* open file for reading */ fp = fopen (*argv, "r"); if (!fp) { fprintf (stderr, "%s: Couldn't open file %s\n", argv[0], argv[1]); exit (1); } /* reading lines from file */ charInLine = getline (fp); while (charInLine != 0) { /* check for lines */ if (checkOccurrence (line)) { count ++; } printf ("Having read line %s\n", line); charInLine = getline (fp); } printf ("The number of lines containing a is %d\n", count); fclose (fp); return 0; }