C Program to Read, Write, and Remove Characters from a File
File handling is one of the most important concepts in C programming. It allows programs to store data permanently, read existing data, and modify file contents efficiently.
In this tutorial, we will learn how to read and write files in C and how to remove a specific character from a file. We will cover step-by-step algorithms, example programs, explanations of file modes, and common mistakes.
1. Understanding File Handling in C
C provides file handling functions in the stdio.h library. To work with files, we use a FILE pointer.
FILE *fp;
Some commonly used file modes:
r → Open file for reading w → Open file for writing (overwrites file) a → Open file for appending r+ → Open file for reading and writing w+ → Create file for reading and writing
Important file functions:
fopen() → Opens a file fclose() → Closes a file fprintf() → Writes formatted data to file fscanf() → Reads formatted data from file fgetc() → Reads a character from file fputc() → Writes a character to file
2. C Program to Write to a File
Writing to a file means storing data permanently in a file instead of displaying it only on the screen.
Algorithm to Write to a File
1. Declare a FILE pointer.
2. Open the file using fopen() in write mode.
3. Check if the file opened successfully.
4. Use fprintf() or fputc() to write data.
5. Close the file using fclose().
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("data.txt", "w");
if(fp == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(fp, "Welcome to File Handling in C.\n");
fprintf(fp, "This file is created using fprintf().\n");
fclose(fp);
printf("Data written successfully.\n");
return 0;
}
Output: Data written successfully. File Content (data.txt): Welcome to File Handling in C. This file is created using fprintf().
3. C Program to Read from a File
Reading from a file allows us to retrieve stored data and display or process it.
Algorithm to Read from a File
1. Declare FILE pointer.
2. Open file in read mode using fopen().
3. Check if file exists.
4. Use fgetc() or fscanf() to read data.
5. Close the file.
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("data.txt", "r");
if(fp == NULL) {
printf("File not found!\n");
return 1;
}
while((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
return 0;
}
Output: Welcome to File Handling in C. This file is created using fprintf().
4. C Program to Remove a Specific Character from a File
To remove a character from a file, we cannot directly delete it. Instead, we create a temporary file, copy all characters except the target character, and then replace the original file.
Algorithm to Remove a Character
1. Open original file in read mode.
2. Open temporary file in write mode.
3. Read each character from original file.
4. If character is not equal to target, write it to temp file.
5. Close both files.
6. Delete original file and rename temp file.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp1, *fp2;
char ch, removeChar;
fp1 = fopen("data.txt", "r");
if(fp1 == NULL) {
printf("File not found!\n");
return 1;
}
fp2 = fopen("temp.txt", "w");
printf("Enter character to remove: ");
scanf(" %c", &removeChar);
while((ch = fgetc(fp1)) != EOF) {
if(ch != removeChar) {
fputc(ch, fp2);
}
}
fclose(fp1);
fclose(fp2);
remove("data.txt");
rename("temp.txt", "data.txt");
printf("Character removed successfully.\n");
return 0;
}
Example: Original File: Hello World Remove character: o Updated File: Hell Wrld
5. Common Mistakes in File Handling
1. Not checking if fopen() returns NULL.
2. Forgetting to close the file using fclose().
3. Using wrong file modes (e.g., using 'r' when file does not exist).
4. Not handling whitespace properly while reading characters.
5. Forgetting to use space before %c in scanf(" %c", &ch).
6. Applications of File Reading and Writing
1. Storing student records in files.
2. Log file generation in software applications.
3. Editing configuration files.
4. Data cleaning tasks such as removing unwanted characters.
Conclusion
File handling in C allows programs to store, retrieve, and modify data permanently. Writing to a file uses modes like 'w' and functions such as fprintf() and fputc(). Reading from a file typically uses 'r' mode with fgetc() or fscanf().
Removing a character from a file requires creating a temporary file and copying all characters except the unwanted one. Mastering these concepts builds a strong foundation for advanced file processing tasks such as record management, data filtering, and log processing.
Codecrown