Skip to main content

Data file and file modes

Short answer

A data file is a permanently stored named collection of data on disk. File modes given to fopen() include "r" (read existing), "w" (write, create/erase), "a" (append), "r+" (read/write existing), "w+" (read/write, create/erase), "a+" (read/append). Add "b" for binary, e.g., "rb", "wb".

What is a data file in C? What are the modes in file handling? Explain briefly.

Data file

A data file is a named collection of data stored permanently on a disk. Programs read data from it and write results to it, so the data stays after the program ends and large data need not be typed again. Files are of two kinds: text files and binary files.

File modes

The mode is given as the second argument of fopen().

ModeMeaning
rOpen existing file for reading; fails if it does not exist
wOpen for writing; creates the file, or erases the old contents
aOpen for appending; writes go at the end, file is created if absent
r+Read and write on an existing file
w+Read and write; creates new / erases old
a+Read and append

For binary files b is added: rb, wb, ab, rb+, wb+, ab+.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 10 questions →