Nearby lessons
66 of 72C - File Modes
File modes tell C how a file should be opened. The most common beginner modes are r for reading, w for writing, and a for appending.
1. Common File Modes
| Mode | Purpose | If the file does not exist | If the file exists |
|---|---|---|---|
r | Read | Opening fails | Reads existing content |
w | Write | Creates the file | Erases old content |
a | Append | Creates the file | Keeps old content and adds at the end |
Important: Use w only when you want to replace the existing file contents.
2. Example of Write and Append Modes
This example writes one line, then appends another line without removing the first one.
Example02
📝 Key Takeaways
- The r mode opens an existing file for reading.
- The w mode creates a file or erases its existing contents.
- The a mode creates a missing file or adds data at the end.
- Always check whether fopen returns NULL.
🧠 Test Your Knowledge
3 QuestionsProgress: 0 / 3