What is File Handling?
- Siddharth Sharma
- Dec 20, 2024
- 3 min read
Introduction to File Handling in C Programming
Working with files is an essential skill in C programming. Files allow us to store and retrieve data for later use, making programs more efficient and useful. This blog will introduce file handling in C, explain how files are structured, and explore different file operations, file types, and functions in an easy-to-understand way.
What is File Handling?
File handling in C involves creating, reading, writing, and manipulating files. Instead of working with data only during program execution, files help store data permanently on your computer.
File Structure in C
Files in C are sequences of bytes stored on disk. They can contain text (human-readable data) or binary (machine-readable data). C provides a variety of functions to manage these files through the <stdio.h> library.
Files are categorized into two types based on their content:
Text Files: Contain readable characters, like .txt files.
Binary Files: Contain raw binary data, like .bin files.
File Handling Functions in C
C provides several functions to perform file operations. Below are the common ones:
1. Opening a File
Files are opened using the fopen function:
FILE *fopen(const char *filename, const char *mode);filename: The name of the file.
mode: Specifies the operation (e.g., read, write).
Modes:
Mode | Description |
r | Opens a file for reading. |
w | Opens a file for writing. |
a | Opens a file for appending. |
rb | Opens a binary file for reading. |
wb | Opens a binary file for writing. |
r+ | Opens a file for reading and writing. |
w+ | Opens a file for writing and reading (overwrites if the file exists). |
a+ | Opens a file for reading and appending. |
Example:
FILE *file = fopen("data.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
}2. Reading and Writing
Writing to a File: Use the fprintf or fwrite functions.
Reading from a File: Use the fscanf or fread functions.
Example: Writing to a Text File
FILE *file = fopen("data.txt", "w");
if (file != NULL) {
fprintf(file, "Hello, File Handling!\n");
fclose(file);
}Example: Reading from a Text File
FILE *file = fopen("data.txt", "r");
if (file != NULL) {
char buffer[100];
while (fgets(buffer, 100, file) != NULL) {
printf("%s", buffer);
}
fclose(file);
}3. Closing a File
Always close a file after performing operations using fclose:
fclose(file);4. Checking for End of File
Use feof to check if the end of the file is reached:
while (!feof(file)) {
// Read data
}File Types in C
C handles two main types of files:
Text Files
Store data in human-readable form.
Operations use functions like fprintf and fscanf.
Binary Files
Store data in machine-readable form.
Operations use functions like fwrite and fread.
Example: Writing to a Binary File
FILE *file = fopen("data.bin", "wb");
if (file != NULL) {
int num = 42;
fwrite(&num, sizeof(int), 1, file);
fclose(file);
}Example: Reading from a Binary File
FILE *file = fopen("data.bin", "rb");
if (file != NULL) {
int num;
fread(&num, sizeof(int), 1, file);
printf("Number: %d\n", num);
fclose(file);
}Common File Handling Functions
Here is a summary of the most commonly used functions:
Function | Purpose |
fopen | Opens a file in a specified mode. |
fclose | Closes an open file. |
fprintf | Writes formatted data to a text file. |
fscanf | Reads formatted data from a text file. |
fwrite | Writes binary data to a file. |
fread | Reads binary data from a file. |
fseek | Moves the file pointer to a specific location. |
ftell | Returns the current position of the file pointer. |
feof | Checks if the end of the file is reached. |
Practical Example: Student Records
Below is an example of how file handling can be used to manage student records:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file = fopen("students.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(file, "ID\tName\tScore\n");
fprintf(file, "1\tAlice\t85\n");
fprintf(file, "2\tBob\t90\n");
fclose(file);
file = fopen("students.txt", "r");
if (file != NULL) {
char buffer[100];
while (fgets(buffer, 100, file) != NULL) {
printf("%s", buffer);
}
fclose(file);
}
return 0;
}Conclusion
File handling in C is a powerful feature that allows programs to interact with data stored on disk. By mastering file operations, you can build programs that create, store, and process data efficiently. Remember to always close files and handle errors gracefully. Practice these concepts to improve your programming skills!




Comments