C File Modes

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is File Modes?

The second argument of fopen selects the mode. "r" reads an existing file, "w" creates or truncates for writing, and "a" appends at the end. Adding + allows both reading and writing, and adding b selects binary mode ("rb", "wb"). Writing modes create the file if it does not exist, except "r" and "r+".

Remember
"r"   read, must exist       "r+"  read/write, must exist
"w"   write, truncate/create  "w+"  read/write, truncate
"a"   append, create          "a+"  read/append
add b for binary: "rb" "wb" "ab"

Cheat Sheet

Keep these facts handy while you work with File Modes in C.

TopicDetail
rFile must exist
wDestroys previous content
aAlways writes at the end
bBinary, no newline translation

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Reading Text Files.

Example 1 — Create, append, and read back

Compare "w" and "a".

C
#include <stdio.h>

int main(void) {
    FILE *fp = fopen("modes.txt", "w");
    if (!fp) return 1;
    fputs("line1\n", fp);
    fclose(fp);

    fp = fopen("modes.txt", "a");
    if (!fp) return 1;
    fputs("line2\n", fp);
    fclose(fp);

    char buf[32];
    fp = fopen("modes.txt", "r");
    if (!fp) return 1;
    while (fgets(buf, sizeof buf, fp)) printf("%s", buf);
    fclose(fp);
    return 0;
}

How It Works

  1. "w" starts the file with line1.
  2. "a" keeps line1 and adds line2 at the end.
  3. Opening with "w" again would erase both lines.

Edge Cases & Pitfalls

Check these before relying on File Modes in real programs.

trunc

Truncating by accident

"w" and "w+" delete existing content immediately.

switch

Switching between read and write

With + modes you must call fflush or a positioning function (fseek, rewind) between a write and a read.

Key Takeaways

  • w: destructive.
  • a: safe for logs.
  • b: use for non-text data.

One line: The second argument of fopen selects the mode.

Frequently Asked Questions

w truncates the file to zero length before writing; a keeps existing content and writes new data at the end.
On Windows, to prevent newline translation for non-text data. On POSIX systems it has no effect but is harmless and portable.
Continue with "C Reading Text Files" next. Read lines and characters with fgets and fgetc. The C File Handling hub lists every topic in order.

Next: Reading Text Files

Read lines and characters with fgets and fgetc.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful