C File Handling
Understanding File Management in C
In C programming, file handling lets you store, retrieve, and manipulate data beyond program execution. This is useful when you need permanent storage, unlike variables that disappear when the program ends.
Types of File Access
C supports different file interaction modes:
- "r" (Read) – Opens an existing file for input.
- "w" (Write) – Creates a new file or clears an existing one to start fresh.
- "a" (Append) – Preserves old data and adds new content at the end.
- "r+" lets you tweak and view an existing file
- "w+"wipes the slate clean for updates and reading, while
- "a+" invites additions without erasing, even starting from scratch if the file’s missing.
Steps for File Operations
1. File Pointer Setup
Use a FILE* type to control file activity.
FILE *fp;
2. Opening a File
Connect the pointer to a file using fopen().
fp = fopen("data.txt", "r");3. Reading or Writing
Depending on your mode, choose functions like fprintf(), fscanf(), fgets(), fputs(), fread(), or fwrite().
4. Closing the File
Always end your session with fclose(fp); to free memory and prevent corruption.
Writing to a File
You can save text to a file using fprintf() or fputs().
FILE *fp = fopen("output.txt", "w");
fprintf(fp, "C handles files well!");
Fclose(fp); Reading from a File
To extract content, fscanf(), fgets(), or fread() can be used.
FILE *fp = fopen("output.txt", "r");
char buffer[100];
fgets(buffer, 100, fp);
printf("%s", buffer);
Fclose(fp); Detecting File Errors
Always check if your pointer is NULL after opening a file.
if (fp == NULL) {
printf("Problem opening file.");
} End-of-File Identification
Use feof() to detect when the end of the file is reached during loops.
while (!feof(fp)) {
// Continue reading
} Binary File Handling
For storing non-text data (e.g., structs), use binary formats:
- fread(&data, sizeof(data), 1, fp);
- fwrite(&data, sizeof(data), 1, fp);
Clearing File Data
To clear all contents, open with mode "w", which automatically wipes previous data.
Common I/O Functions
| Function | Purpose |
|---|---|
| fopen() | Starts file access |
| fclose() | Shuts file connection |
| fgetc() | Reads a single character |
| fputc() | Writes a single character |
| fgets() | Reads a line from file |
| fputs() | Writes a string to file |
| fprintf() | Outputs formatted text |
| fscanf() | Inputs formatted data |
Buffer Flushing
Sometimes, use fflush(fp); to clear internal buffers manually—especially useful before closing a file.
Practical Example
#include <stdio.h>
int main() {
FILE *fp = fopen("log.txt", "a+");
if (fp == NULL) {
printf("Unable to open the file.\n");
return 1;
}
fprintf(fp, "Session started.\n");
char line[128];
rewind(fp); // Move pointer to start for reading
while (fgets(line, sizeof(line), fp)) {
printf("%s", line);
}
fclose(fp);
return 0;
} Why Learn File Handling?
- Enables persistent storage
- Makes saving/loading game states, logs, or data files possible
- Essential for real-world applications (like databases, config files, etc.)
Prefer Learning by Watching?
Watch these YouTube tutorials to understand C Tutorial visually:
What You'll Learn:
- 📌 #29: C File Handling | [2025] C Programming for Beginners
- 📌 What Is File Handling In C? | File Handling In C Programming | C Programming Tutorial | Simplilearn