←back to thread

71 points susam | 1 comments | | HN request time: 0.219s | source
1. kazinator ◴[] No.43674317[source]
Pascal taught a generation of coders that you test for end-of-file before trying to read any data from the stream.

When they hit C, they wrote code around fgets that processes the last line twice:

  while (!feof(instream)) {
    fgets(linebuf, sizeof linebuf, instream);
    process_line(linebuf);
  }
Or processes the EOF value from getc as a character:

  while (!feof(instream)) {
    char ch = getc(instream);
    switch (ch) ...
  }