C Notes
Complete guide to file modes in C — r, w, a, r+, w+, a+ modes, binary modes rb/wb/ab, mode comparison table, choosing the right mode, practical examples with output.
Choosing the wrong file mode is a common source of bugs — accidentally truncating important data with "w" when you meant "a", or getting NULL because you used "r" on a nonexistent file. This guide covers every file mode in C with clear examples.
All File Modes at a Glance
| Mode | Type | Read | Write | Create | Truncate | Position |
|---|---|---|---|---|---|---|
"r" | Text | ✅ | ❌ | ❌ | ❌ | Beginning |
"w" | Text | ❌ | ✅ | ✅ | ✅ | Beginning |
"a" | Text | ❌ | ✅ | ✅ | ❌ | End |
"r+" | Text | ✅ | ✅ | ❌ | ❌ | Beginning |
"w+" | Text | ✅ | ✅ | ✅ | ✅ | Beginning |
"a+" | Text | ✅ | ✅ | ✅ | ❌ | End (writes) |
"rb" | Binary | ✅ | ❌ | ❌ | ❌ | Beginning |
"wb" | Binary | ❌ | ✅ | ✅ | ✅ | Beginning |
"ab" | Binary | ❌ | ✅ | ✅ | ❌ | End |
"rb+" | Binary | ✅ | ✅ | ❌ | ❌ | Beginning |
"wb+" | Binary | ✅ | ✅ | ✅ | ✅ | Beginning |
"ab+" | Binary | ✅ | ✅ | ✅ | ❌ | End (writes) |
Mode "r" — Read Only
Use when: You only need to read existing data. Safest mode — can't accidentally corrupt the file.
Mode "w" — Write Only (DANGER: Truncates!)
#include <stdio.h>
int main() {
// WARNING: If file exists, ALL content is erased!
FILE *fp = fopen("output.txt", "w");
if (fp == NULL) return 1;
fprintf(fp, "This is new content.\n");
fprintf(fp, "Previous content is GONE.\n");
fclose(fp);
printf("Written (previous content destroyed).\n");
return 0;
}Written (previous content destroyed).
Use when: You want to create a fresh file or regenerate output from scratch.
Mode "a" — Append Only
Log entry appended.
Use when: Adding to existing content — log files, history, queues.
Mode "r+" — Read and Write (Update)
Use when: You need to read existing data AND write modifications. File must already exist.
Mode "w+" — Read and Write (Fresh Start)
Written and read back: Line 1 Line 2 Line 3
Use when: You need a temporary read-write file, or you're regenerating content you'll also need to read.
Mode "a+" — Read and Append
Use when: You need to read the entire file AND add to it without destroying content.
Binary Modes
Add "b" to any mode for binary:
Loaded: id=1, name=Test, value=99.5
Choosing the Right Mode — Decision Flowchart
| └─ YES | Does file exist? → YES → "r" |
| └─ Keep existing data? | YES → "a" (append) |
| └─ Keep existing data? | YES → "r+" (must exist) or "a+" (creates if missing) |
| └─ Add "b" to any of the above | "rb", "wb", "ab", "rb+", etc. |
Interview Questions
Q1: What happens if you open a nonexistent file with "w" mode?
The file is automatically created. "w" mode always creates the file if it doesn't exist, and truncates it to zero length if it does exist.
Q2: What's the difference between "r+" and "w+"?
"r+" requires the file to exist (returns NULL otherwise) and preserves content. "w+" creates the file if missing and truncates if it exists. Both allow reading and writing.
Q3: In "a+" mode, where do reads start and where do writes go?
In "a+" mode, you can seek anywhere for reading, but ALL writes are forced to the end of the file regardless of the current file position. This is enforced by the OS.
Q4: Why is binary mode important on Windows?
On Windows, text mode translates \n to \r\n on output and \r\n to \n on input. This corrupts binary data (images, structs). Binary mode ("rb"/"wb") disables this translation, reading/writing bytes exactly as they are.
Summary
- "r" — read only, file must exist
- "w" — write only, creates/truncates (DESTROYS existing data!)
- "a" — append only, creates if missing, preserves existing data
- "r+/w+/a+" — read+write variants with different creation/truncation behavior
- Add "b" for binary mode — essential for non-text data on Windows
- Always pick the most restrictive mode that meets your needs
- When in doubt, use "r" for reading and "a" for adding — both are safe
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for File Modes in C – Complete Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, file, handling, modes, file modes in c – complete guide
Related C Programming Topics