---
title: "Data file and file modes"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:data-file-modes"
source: https://www.bctengineer.com/q/semester_1/computer_programming/data-file-modes
site: BCT Engineering
---

# Data file and file modes

Computer Programming · Chapter 10: Data Files

- Asked in: 2072 Chaitra, 1+3 marks
- Video: [file opening modes in c | Reading & Writing | Append modes | C Programming | Lec-64](https://www.youtube.com/watch?v=nHqkiSulF6Q) — Education 4u
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/data-file-modes
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

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

## 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".

## Answer

#### 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()`.

| Mode | Meaning |
|---|---|
| `r` | Open existing file for reading; fails if it does not exist |
| `w` | Open for writing; creates the file, or erases the old contents |
| `a` | Open 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+`.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is a data file?
   A. A named collection of data stored permanently on disk
   B. Temporary data in RAM
   C. Only an executable
   D. A header file
   Answer: A. Data in a file stays after the program ends. (Key point: Data file)

2. What does mode "a" do?
   A. Erases the file
   B. Appends at the end and creates the file if absent
   C. Fails if the file is missing
   D. Reads only
   Answer: B. Writes go at the end and old data is kept. (Key point: Mode a)

3. What happens with "r" if the file does not exist?
   A. It is created
   B. It fails
   C. It is erased
   D. It is appended
   Answer: B. "r" opens an existing file for reading only; fopen returns NULL otherwise. (Key point: Mode r)

4. How is a binary mode written for write access?
   A. wb
   B. bw+
   C. w
   D. tb
   Answer: A. b is added to the mode, giving rb, wb, ab and so on. (Key point: Binary mode)

## Related in Computer Programming

- [Opening and closing a data file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aopen-close-file) — Computer Programming, chapter 10, asked 2×
- [Copy contents of one file to another](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-copy-contents) — Computer Programming, chapter 10, asked 2×
- [Store 48 students in file with append](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-48-students-append) — Computer Programming, chapter 10
- [Employee text file; sort into binary file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-employee-sorted-binary) — Computer Programming, chapter 10
- [INVENTORY file of items](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-inventory) — Computer Programming, chapter 10
- [Display records sorted by name from file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-sorted-records-by-name) — Computer Programming, chapter 10

## Related in other subjects

- [File system handling of kernel](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Akernel-file-system-handling) — Embedded System, chapter 6
- [Handling voluminous data](https://www.bctengineer.com/question.md?key=semester_8%2Finformation_system%3Avoluminous-data-techniques-challenges) — Information System, chapter 8
- [Instruction and data format, addressing modes](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3A8085-instruction-data-format-addressing) — Microprocessor, chapter 2, asked 2×
- [Modes of parallel data transfer](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aparallel-data-transfer-modes) — Microprocessor, chapter 4
