Types of File in C (Text Files and Binary Files) | File Handling in C Programming
Learn the two main types of file in C — text files and binary files. Understand their differences, storage methods, and examples of reading and writing files in C programming.
Introduction: Understanding File Handling in C
In C programming, a file is a storage unit that allows a program to store and retrieve data permanently on a disk.
- Unlike variables that lose data after program execution, files provide a way to save data permanently for later use.
The C language supports two main types of files:
- Text Files
- Binary Files
Understanding both types is crucial for building real-world applications like databases, billing systems, and user data storage.
1️⃣ Text Files in C
A text file stores data in a human-readable format (ASCII characters).
It can be opened, viewed, or edited using any text editor such as Notepad or VS Code.
Text files are ideal when the data doesn’t require strict formatting or precision — for example, writing simple messages, names, or numbers.
How Text Files Work
Each character (letter, number, space, or symbol) in a text file is stored as ASCII code.
When reading or writing, data is automatically converted between characters and ASCII.
Example: Writing to a Text File in C
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("textfile.txt", "w"); // Open in write mode
if (fp == NULL) {
printf("Error opening file!");
return 1;
}
fprintf(fp, "Hello World!\n");
fprintf(fp, "Welcome to Text File Handling in C.\n");
fclose(fp);
printf("Data written successfully to text file!");
return 0;
}Output:
Creates a file named textfile.txt with the following content:
Hello World!
Welcome to Text File Handling in C.Advantages of Text Files
- Easy to read and edit manually
- Portable across different systems
- Simple for storing textual or numerical data
Limitations
- Slower to read/write compared to binary files
- Takes more storage space due to ASCII conversion
- May cause data loss in precision for complex data types
2️⃣ Binary Files in C
A binary file stores data in machine-readable binary format (0s and 1s).
It is more compact, faster, and better for storing structured or large datasets such as images, records, or objects.
How Binary Files Work
Binary files store data exactly as it is in memory — without any conversion to ASCII.
This makes them ideal for high-speed data processing and complex data storage.
Example: Writing to a Binary File
#include <stdio.h>
struct Student {
char name[30];
int roll;
float marks;
};
int main() {
struct Student s1 = {"John Doe", 101, 89.5};
FILE *fp;
fp = fopen("student.dat", "wb"); // Open in binary write mode
if (fp == NULL) {
printf("Error opening file!");
return 1;
}
fwrite(&s1, sizeof(s1), 1, fp); // Write structure to file
fclose(fp);
printf("Data written successfully to binary file!");
return 0;
}Explanation:
fwrite()writes raw bytes directly to the file.- Data is stored exactly as it appears in memory.
Advantages of Binary Files
- Faster read/write operations
- Efficient storage (no conversion overhead)
- Ideal for storing records, images, and large datasets
Limitations
- Not human-readable
- Harder to debug manually
- Platform-dependent data representation
Difference Between Text and Binary Files in C
| Feature | Text File | Binary File |
|---|---|---|
| Data Format | Human-readable (ASCII) | Machine-readable (Binary) |
| Speed | Slower | Faster |
| Storage Size | Larger | Smaller |
| Editing | Easy (any text editor) | Not editable manually |
| Accuracy | May lose precision | Stores exact binary data |
| Use Case | Logs, reports, configuration | Images, structures, databases |
Common File Modes Used
| Mode | Description |
|---|---|
"r" | Read text file |
"w" | Write text file |
"a" | Append text file |
"rb" | Read binary file |
"wb" | Write binary file |
"ab" | Append binary file |
Best Practices for File Handling in C
- Always check if the file is successfully opened (
if (fp == NULL)). - Close files using
fclose(fp)to free system resources. - Use binary files for performance-critical applications.
- Use text files when readability is important.
- Avoid mixing text and binary operations in the same file.
Conclusion
The two types of files in C — text files and binary files — play a vital role in handling data efficiently.
Text files are best for simple readable data, while binary files are ideal for fast, compact, and structured storage.
Understanding when and how to use each type of file is a key skill for every C programmer aiming to build scalable and data-driven applications.
