File handling in C allows programs to store data permanently on storage devices such as HDDs, SSDs, and USB drives.
Using files, you can read and write large amounts of data without losing it after program termination.
C provides a rich set of functions in the <stdio.h> library to perform file-related tasks.
1. File Operations in C
The main file operations in C are:
- Creating and opening a file
- Reading from a file
- Writing into a file
- Closing a file
- Appending data
- Checking for end-of-file
- Error handling
Before working with files, we use a file pointer, a special pointer of type FILE.
Syntax
FILE *fp;
1.1 Opening a File
Files are opened using fopen().
Syntax
FILE *fopen(const char *filename, const char *mode);
Common Modes
| Mode | Meaning |
|---|---|
"r" | Read (file must exist) |
"w" | Write (creates or overwrites) |
"a" | Append (writes to end) |
"r+" | Read + Write |
"w+" | Write + Read |
"a+" | Append + Read |
Example
FILE *fp = fopen("data.txt", "w");
1.2 Writing to a File
C provides several functions:
fprintf()
fprintf(fp, "Hello File!");
fputs()
fputs("This is C File Handling.\n", fp);
fputc()
fputc('A', fp);
Example (Complete Writing)
#include <stdio.h>
int main() {
FILE *fp = fopen("output.txt", "w");
fprintf(fp, "Welcome to Savanka C Tutorials!");
fclose(fp);
return 0;
}
1.3 Reading from a File
Use these functions:
fscanf()
fscanf(fp, "%d", &num);
fgets()
fgets(str, 50, fp);
fgetc()
char ch = fgetc(fp);
Example
#include <stdio.h>
int main() {
FILE *fp = fopen("output.txt", "r");
char line[100];
while (fgets(line, sizeof(line), fp)) {
printf("%s", line);
}
fclose(fp);
return 0;
}
1.4 Closing a File
Always close files after processing:
fclose(fp);
It prevents:
- Data loss
- Memory leak
- File corruption
1.5 Appending to a File
Append mode keeps existing content and adds new text at end.
FILE *fp = fopen("output.txt", "a");
fprintf(fp, "\nNew line added!");
1.6 Checking End of File (EOF)
while (!feof(fp)) {
// read file
}
Or more correctly:
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
2. Processing a Data File
Processing a data file means reading existing content, analyzing it, modifying it, or writing new data.
2.1 Writing Structured Data to a File
Example: Storing student records.
#include <stdio.h>
struct Student {
int roll;
char name[20];
float marks;
};
int main() {
FILE *fp = fopen("students.txt", "w");
struct Student s = {1, "Sagar", 89.5};
fprintf(fp, "%d %s %.2f\n", s.roll, s.name, s.marks);
fclose(fp);
return 0;
}
2.2 Reading Structured Data from a File
#include <stdio.h>
struct Student {
int roll;
char name[20];
float marks;
};
int main() {
FILE *fp = fopen("students.txt", "r");
struct Student s;
while (fscanf(fp, "%d %s %f", &s.roll, s.name, &s.marks) != EOF) {
printf("Roll: %d | Name: %s | Marks: %.2f\n",
s.roll, s.name, s.marks);
}
fclose(fp);
return 0;
}
2.3 Counting Words, Characters or Lines in a File
Example: Count lines
int count = 0;
char ch;
FILE *fp = fopen("file.txt", "r");
while ((ch = fgetc(fp)) != EOF) {
if (ch == '\n') count++;
}
fclose(fp);
printf("Total Lines: %d", count);
2.4 Copying Data from One File to Another
FILE *src = fopen("a.txt", "r");
FILE *dest = fopen("b.txt", "w");
char ch;
while ((ch = fgetc(src)) != EOF) {
fputc(ch, dest);
}
fclose(src);
fclose(dest);
2.5 Updating File Records
You can:
- Modify specific lines
- Rewrite entire files
- Append summaries or logs
For example, updating marks in a student database requires reading file → modifying → rewriting.
Conclusion
File handling is essential for creating applications that store and process data permanently. With fopen(), fprintf(), fscanf(), fclose(), and other functions, C gives complete control over file creation, reading, writing, and updating.
📚 Citations
🔗 View other articles about C Programming:
https://savanka.com/category/learn/c-programming/
🔗 External C Documentation:
https://www.w3schools.com/c/