Skip to content
CurriculumChapter 29 · Files, Bits & Utilities
IntermediateHigh focus~14 min

FILE Pointer, fopen & Modes

Files turn a program into something that can remember. The FILE pointer is your handle, and the mode decides whether you read, write, or append.

In plain English

A FILE pointer is a bookmark in a notebook. The mode says whether you may read, write, or both.

01

The FILE pointer and fopen modes

stdio.h defines the FILE type and the functions that operate on streams. On many implementations FILE is a structure with fields for the buffer, current position, error indicator, and end-of-file indicator. The C standard treats those members as opaque: use ferror, feof, ftell, and fseek instead of reading them directly. fopen opens a file and returns a FILE pointer, or NULL on failure. Common modes are "r" for reading, "w" for writing (truncates), "a" for appending, "r+" for reading and writing, and "rb"/"wb" for binary. Always check the result before using the pointer.

chapter-29.cC
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) {
    perror("fopen");
    return 1;
}
fprintf(fp, "hello file\n");
fclose(fp);
02

Closing, flushing, and errors

fclose releases the stream and flushes buffered output. Failing to close a file can lose data that is still in the buffer. ferror reports whether a stream error has occurred, and perror prints a human-readable message based on errno. The FILE structure itself is opaque; use the standard functions instead of poking at its members.

chapter-29.cC
FILE *fp = fopen("data.txt", "r");
if (!fp) {
    perror("data.txt");
    return 1;
}

/* read or write here */

if (fclose(fp) != 0) {
    perror("fclose");
}
03

Inside the FILE handle and file positioning

The standard treats FILE as an opaque object, but the concept is a stream state: a buffer, a current position, an error indicator, an end-of-file indicator, and mode/ownership flags. Do not access the fields directly. Use feof and ferror to read the indicators, clearerr to reset them, ftell to ask for the position, fseek to move it, and rewind to return to the beginning.

chapter-29.cC
FILE *fp = fopen("data.txt", "rb");
if (fp) {
    fseek(fp, 0, SEEK_END);
    long size = ftell(fp);
    rewind(fp);

    printf("size = %ld bytes\n", size);
    clearerr(fp);
    fclose(fp);
}

Watch it happen

Step through the memory changes one instruction at a time.

step-by-step

Step 1 of 7

Open for writing

fopen("data.txt", "w") creates a stream. Mode "w" truncates any existing file.

FILE stream
"w"
buffer
empty
disk · data.txt
empty

Key takeaways

  • fopen returns NULL on failure; always check it.
  • Mode "w" truncates, "a" appends, "r" reads.
  • fclose flushes buffered output and releases the stream.
  • ferror and perror help diagnose I/O failures.
  • FILE is opaque stream state: use feof, ferror, clearerr, ftell, fseek, and rewind.

Common pitfalls

  • Using a FILE pointer without checking for NULL.
  • Forgetting fclose and losing buffered writes.
  • Using "w" when you meant "a" and erasing the file.
  • Reading or writing FILE fields directly; the layout is not portable.

Key focus

  • Know the fopen mode strings and their effects.
  • Know the standard functions that expose FILE state: feof, ferror, clearerr, ftell, fseek, rewind.
  • Expect questions on fopen failure and fclose.

Quick check

0 / 2 answered

1. What does fopen return when it cannot open a file?

2. Which mode opens a file for appending without truncating it?