Skip to content
FollowtheDamnCode

Opening and Closing Files

fopen, fclose, and file modes.

5 minute read

Big idea

Variables vanish when a program ends. Files keep data on disk. You open a file with fopen, use it, and close it with fclose.

The idea

FILE *f = fopen("notes.txt", "w");
if (f == NULL) {
    printf("Couldn't open the file\n");
    return 1;
}
// … use the file …
fclose(f);
  • FILE * — a pointer to a FILE, C's handle for an open file. You never look inside it; you pass it to file functions.
  • "notes.txt" — the file's name (a path).
  • "w" — the mode: what you want to do with it.
  • fopen returns NULL if it fails — the file doesn't exist, the folder isn't writable, and so on. Always check.
  • fclose(f) — finishes any pending writes and releases the file.

Modes

ModeMeansIf the file existsIf it doesn't
"r"readread from the startfails (NULL)
"w"writeerases it firstcreates it
"a"appendadds to the endcreates it

Careful with "w" — opening an existing file for writing wipes its contents immediately.

Try it

This writes a file, then opens it again and reads it back. The file lives in a scratch folder that's cleaned up after the run.

Write a file, then read it back
#include <stdio.h>

int main(void) {
    FILE *out = fopen("greeting.txt", "w");
    if (out == NULL) {
        printf("Couldn't create the file\n");
        return 1;
    }
    fprintf(out, "Hello from a file!\n");
    fclose(out);

    FILE *in = fopen("greeting.txt", "r");
    if (in == NULL) {
        printf("Couldn't open the file\n");
        return 1;
    }
    char line[100];
    fgets(line, sizeof line, in);
    printf("Read back: %s", line);
    fclose(in);
    return 0;
}
output
Read back: Hello from a file!

Run and edit it in the app.

Watch it

What NULL looks like

Predict: opening a file that isn't there
#include <stdio.h>

int main(void) {
    FILE *f = fopen("does-not-exist.txt", "r");
    if (f == NULL) {
        printf("no file\n");
    } else {
        printf("opened\n");
        fclose(f);
    }
    return 0;
}
What does it print? Show the answer
no file

Run and edit it in the app.

perror("fopen") prints a message explaining why it failed — handy while debugging.

Common mistake

Using the file without checking for NULL. Writing to a NULL FILE * crashes the program. The check is two lines; always write it.

Think of it like

A library book: you check it out (fopen), read or annotate it, then return it (fclose). And "w" mode is borrowing a notebook — someone rubs out every page before handing it to you.

🕷️ Spidey-sense: My photo archive for the Bugle. Open the folder, drop in the shots, close it — and never open last month's folder in "w" mode. Jameson would not accept "I erased the evidence."

Key terms

  • File — Named data stored on disk.
  • FILE * — A handle to an open file.
  • fopen / fclose — Open and close a file.
  • Mode — What you'll do with the file: "r", "w", "a" (and "r+" etc. for both).

Check yourself

What does fopen return when it fails?

What does "w" mode do to an existing file?

Which mode adds to the end of a file?