liveBook · Manning

Streaming

As explained in Modern C, Third Edition

A stream is an abstract C input or output sequence associated with a file, device, or other data source or destination.

A stream is an abstract C input or output sequence associated with a file, device, or other data source or destination. The opaque FILE type represents a stream through library interfaces rather than accessible implementation details. FILE* values can be tested for null to determine whether opening or reassociating a stream succeeded. section 8.4.1 section 8.4.2

Unformatted output uses fputc and fputs, which generalize putchar and puts to an explicitly selected stream. stdout usually targets the terminal for ordinary output, while stderr provides a separate channel for urgent or diagnostic output. These functions return EOF on failure, so callers can propagate write errors. section 8.4.1

Opening and managing file streams requires choosing a mode and checking each operation:

  • Use fopen(path, mode) to create or open a stream; test the returned FILE* before writing. section 8.4.2
  • Choose "a" to write at end of file, "w" to create or wipe a file, and "r" to read an existing file. section 8.4.2
  • Add "+" for update access, "b" for binary treatment, or "x" to require that a newly written file does not already exist. section 8.4.2
  • Use freopen to associate an existing stream with another file, fclose to close it, and fflush to flush it. section 8.4.2

Append a log record

FILE* logfile = fopen("mylog.txt", "a"); if (!logfile) { perror("fopen failed"); return EXIT_FAILURE; } fputs("feeling fine today\n", logfile); return EXIT_SUCCESS; The same stream-specific idea applies to formatted output: fprintf accepts a FILE* before its format string. Its trailing arguments must correspond exactly to the format specifiers; use formats such as "%d", "%u", "%x", and "%g" for the represented value. section 8.4.2 section 8.4.4

int putchar_manually(int c) {
  return fputc(c, stdout);
}
int puts_manually(char const s[static 1]) {
  if (fputs(s,    stdout) == EOF) return EOF;
  if (fputc('\n', stdout) == EOF) return EOF;
  return 0;
}

Compare terminal output through stdout with the equivalent fputc and fputs calls that explicitly receive a stream. section 8.4.1

How Modern C, Third Edition covers it

How deep it goes
Explains FILE* abstraction, output errors, file modes, lifecycle operations, and format constraints, while deferring opaque implementation details.
Examples
Uses putchar and puts equivalents, an append-mode logfile, freopen redirection, and fprintf format tables.
What it assumes
Assumes basic C functions, pointers, return values, and character strings, while introducing FILE* and opaque-type interfaces incrementally.
What you can do after
Operate/configure C streams by opening files, selecting modes, directing output, checking failures, and closing or flushing resources.
How it connects
Connects streams with files, standard output, standard error, opaque types, formatted output, and error handling.

Also explained in

Compare how 19 books explain streaming →

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.