liveBook · Manning

End-of-file indicator

As explained in Modern C, Third Edition

End-of-file is the condition indicating that no more input is available from a stream.

End-of-file indicates that no more input is available from a stream. In C, fgetc returns int so it can represent every character and the special value EOF. A returned EOF does not by itself prove that the stream ended: after a failed read, call feof to distinguish end-of-file from an input error. section 8.4.5

Checking a character read

A loop must treat EOF as a status, not as a character: read with fgetc, test for EOF, then use feof(stream) to confirm end-of-file or handle the error otherwise. The same distinction appears when implementing fgets: EOF plus feof ends the input; EOF without feof reports failure. section 8.4.5

EOF on output

For putchar, fputc, puts, and related output functions, EOF signals failure rather than the end of an input stream. Check the return value and use perror when a diagnostic is needed. section 8.4.1 section 8.1.3

Listing 8.2 Implementing fgets in terms of fgetc
char* fgets_manually(char s[restrict], int n,
                     FILE*restrict stream) {
  if (!stream) return nullptr;
  if (!n) return s;
  /* Reads at most n-1 characters */
  for (size_t i = 0; i < n-1; ++i) {
    int val = fgetc(stream);
    switch (val) {
      /* EOF signals end-of-file or error */
      case EOF: if (feof(stream)) {
        s[i] = 0;
        /* Has been a valid call */
        return s;
      } else {
        /* Error */
        return nullptr;
      }
      /* Stop at end-of-line. */
      case '\n': s[i] = val; s[i+1] = 0; return s;
      /* Otherwise just assign and continue. */
      default: s[i] = val;
    }
  }
  s[n-1] = 0;
  return s;
}

Trace how a bounded line reader separates end-of-file from input errors after fgetc returns EOF. section 8.4.5

How Modern C, Third Edition covers it

How deep it goes
Explains return-value encoding, failed-read timing, feof confirmation, and output-error handling, but not stream internals.
Examples
Uses manual fgets logic, character and line input, output checks, and a program that dumps files with fgets.
What it assumes
Assumes basic C functions and streams while defining FILE pointers, return-value conventions, and opaque stream interfaces.
What you can do after
Diagnose stream reads by checking EOF, confirming end-of-file with feof, and reporting other failures separately.
How it connects
Connects EOF with streams, fgetc, fgets, feof, error handling, perror, and errno.

Also explained in

Compare how 2 books explain end-of-file indicator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.