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


















