File Modes
1. Introduction
In C programming, file modes specify how a file should be opened. When a file is opened using the fopen() function, a file mode is provided to tell the compiler whether the file will be used for reading, writing, appending, or both.
Correct use of file modes is very important because:
-
It controls access to the file
-
It prevents accidental data loss
-
It ensures proper file operations
2. fopen() Function and File Modes
The fopen() function is used to open a file.
Syntax
-
"filename"→ Name of the file -
"mode"→ File mode (string)
3. Read Mode ("r")
Description
-
Opens a file for reading only
-
File must already exist
Syntax
Important Points
-
File pointer is placed at the beginning
-
If file does not exist,
fopen()returnsNULL
Example
Use
-
Reading data from text files
4. Write Mode ("w")
Description
-
Opens a file for writing only
-
Creates a new file if it does not exist
-
Deletes old data if file already exists
Syntax
âš Warning:
-
Existing file content will be erased
Use
-
Creating new files
-
Writing fresh data
5. Append Mode ("a")
Description
-
Opens a file for adding data at the end
-
Old data is preserved
-
Creates a new file if it does not exist
Syntax
Use
-
Adding records without deleting old data
6. Read and Write Mode ("r+")
Description
-
Opens a file for both reading and writing
-
File must exist
-
File pointer starts at the beginning
Syntax
Use
-
Modifying existing file content
7. Write and Read Mode ("w+")
Description
-
Opens a file for writing and reading
-
Creates a new file if it does not exist
-
Deletes old data if file exists
Syntax
Use
-
Creating new files with read/write access
8. Append and Read Mode ("a+")
Description
-
Opens a file for reading and appending
-
Old data remains unchanged
-
File pointer is at the end for writing
Syntax
Use
-
Reading and adding data safely
9. Binary File Modes
Binary file modes are used when working with binary files.
Common Binary Modes
| Mode | Meaning |
|---|---|
"rb" |
Read binary |
"wb" |
Write binary |
"ab" |
Append binary |
"rb+" |
Read and write binary |
"wb+" |
Write and read binary |
"ab+" |
Append and read binary |
10. Text vs Binary Modes
| Text Mode | Binary Mode |
|---|---|
| Human readable | Not human readable |
| Slower | Faster |
| Uses characters | Uses raw data |
"r", "w", "a" |
"rb", "wb", "ab" |
11. Checking File Opening Error
Always check if the file is opened successfully.
Example
12. File Pointer Position
| Mode | Pointer Position |
|---|---|
"r" |
Beginning |
"w" |
Beginning |
"a" |
End |
"r+" |
Beginning |
"w+" |
Beginning |
"a+" |
End (for writing) |
13. Common Mistakes in File Modes
-
Using
"r"when file does not exist -
Using
"w"instead of"a"(data loss) -
Forgetting
+for read/write access -
Using text mode instead of binary mode
14. Advantages of Using Correct File Modes
-
Prevents accidental data loss
-
Ensures correct file operations
-
Improves program reliability
-
Makes file handling efficient
15. Summary of File Modes
| Mode | Purpose |
|---|---|
"r" |
Read only |
"w" |
Write only |
"a" |
Append |
"r+" |
Read and write |
"w+" |
Write and read |
"a+" |
Append and read |
16. Conclusion
File modes in C determine how a file is accessed and modified. Choosing the correct file mode is essential for safe and efficient file handling. Understanding file modes helps programmers avoid data loss and perform accurate file operations in real-world applications.