CHAPTER 22 / 26
C File Handling
Learn the basics of reading from and writing to files.
01 — Syntax
FILE *file = fopen("file.txt", "r");02 — Example
#include <stdio.h>
int main() {
FILE *file = fopen("aifa.txt", "w");
if (file != NULL) {
fprintf(file, "Hello AIFA");
fclose(file);
printf("File operation completed.");
}
return 0;
}03 — Important Points
- Always close files after use.
- Check whether fopen() returned NULL.
04 — Common Mistakes
- Using a NULL file pointer.
- Forgetting fclose().
05 — Real-World Use
File handling is used for persistent data, configuration and reports.
06 — Practice
Create a small program that demonstrates the main idea of C File Handling.