files

FILE *f = tmpfile();
if (f == NULL) {
    return;
}
fputs("line\n", f);
rewind(f);
char line[16] = {0};
fgets(line, sizeof line, f);
fclose(f);

That ran end to end

Streams

FILE * is a stream handle Open modes matter: r , w , a , and their binary variants Writing with w truncates

Check returns

fopen can fail fputs can fail fgets returns NULL at end of file or on error Check which one happened

Temporary files

tmpfile creates and opens a temporary file that is removed on close Use it for scratch data Do not build predictable temp names by hand

Binary

Use fread and fwrite for binary data Do not parse structs straight from disk Padding and endianness will eventually betray you

Next

Safety in 11 safety

Posted