Files and streams.
Understand fopen, read/write counts, EOF, positioning, and resource cleanup.
The idea behind the syntax.
fopen connects a stream to a named file and returns a FILE pointer or null on failure. The mode determines whether the program reads, writes, appends, and whether it treats bytes in text or binary mode. A successful open creates a resource that should be closed with fclose. Closing also gives buffered output a chance to be written and may report an error that matters to the caller.
Reading from a stream is a sequence of operations, not a promise that all requested data arrives at once. fgetc returns either a character value converted to unsigned char or EOF, so its result belongs in an int. fread and fwrite return element counts that must be compared with requested counts. feof and ferror distinguish end-of-file from an I/O error after a read attempt has reported a short result.
What C guarantees.
Text and binary modes may behave differently on some platforms, especially for line endings. A portable binary format must define its own byte order, widths, and field encoding instead of writing native integers or raw structs. fseek and ftell can move within a seekable stream, but their exact interpretation for text streams is constrained; use positions returned by ftell when returning to a text location.
The example uses tmpfile to obtain a temporary binary stream, writes one short string as bytes, rewinds, reads it back into a separate buffer, and closes the stream. The write and read counts are checked. This is still a resource operation that can fail, even though no permanent path is involved.
Why the detail matters.
A FILE pointer identifies a library-managed stream, not necessarily a raw operating-system file descriptor. The library may buffer data and translate text-mode line endings. fread and fwrite count completed elements, not always bytes unless the element size argument is 1. Their return values need to be compared to the request, and a short read may mean normal end-of-file or an error. ferror and feof are meaningful after an operation indicates that distinction is needed.
The lifetime of a stream begins with a successful open and ends when fclose completes. Every early return between those points needs a cleanup path. For output, checking fclose matters because buffered data may fail only when flushed. A temporary file is still a file resource with possible errors. For a persistent report, a robust design often writes a new file, checks it, then replaces the old one using platform-specific filesystem guarantees rather than assuming one write call was atomic.
See the rule in context.
#include <stdio.h>
#include <string.h>
int main(void) {
FILE *file = tmpfile();
if (file == NULL) return 1;
const char message[] = "C streams";
size_t n = strlen(message);
if (fwrite(message, 1, n, file) != n) { fclose(file); return 1; }
rewind(file);
char copy[16] = {0};
if (fread(copy, 1, n, file) != n) { fclose(file); return 1; }
puts(copy);
return fclose(file) == 0 ? 0 : 1;
}tmpfile creates a temporary stream. strlen measures nine bytes in C streams, excluding the terminator. fwrite stores those nine bytes; rewind returns to the beginning; fread fills the first nine bytes of copy. The array was zero-initialized, so copy remains terminated and puts prints C streams. fclose releases the stream and its return value determines the exit status.
Where the rule stops.
A successful write into a buffered stream is not necessarily a durable disk write. Check fclose and, where durability matters, use platform-specific guarantees beyond the basic C library.
Keep this model.
A file path, process argument, or operating-system handle may be outside ISO C's portable model. The next chapter separates the standard library's promises from system interfaces that deliberately depend on one platform.