Types of File Writing in C Text and Binary
In C programming, there are two primary types of file writing operations based on the type of data being written to the file: Text file writing and Binary file writing. Understanding these two types is essential for effectively working with files in C, as they differ in how they handle data, particularly when it comes to formatting and storage.
At SamagraCS Educational Technology, we’ll guide you through the two types of file writing in C, with clear examples and explanations to help you master the concept.
1. Text File Writing
Text file writing involves writing human-readable data, such as strings, numbers, and characters, to a file in a formatted manner. The data is stored as text, and you can easily open and read these files using any text editor (e.g., Notepad, VS Code).
Key Characteristics:
- Data is stored in a human-readable format.
- The file contents can be viewed and modified using any text editor.
- Data is formatted with newline characters (
\n
) and other separators. - Text files are suitable for storing configuration files, reports, logs, or plain data.
Real-Life Analogy:
Think of a text file as a notebook where you write down notes, with each line being easily readable by anyone who looks at the notebook.
Text File Writing Functions in C
Here are the main functions used for writing to text files in C:
fprintf()
: Used for writing formatted data to a file.fputs()
: Used for writing strings to a file.fputc()
: Used for writing individual characters to a file.
Example of Writing to a Text File Using fprintf()
:
#include <stdio.h>
int main() {
FILE *filePointer;
filePointer = fopen("student.txt", "w"); // Open file in write mode
if (filePointer == NULL) {
printf("Error opening file!\n");
return 1;
}
// Writing formatted data to the file
fprintf(filePointer, "Name: Pawan Jaiswal\n");
fprintf(filePointer, "Age: 21\n");
fprintf(filePointer, "Grade: A\n");
// Closing the file
fclose(filePointer);
printf("Data written to file successfully.\n");
return 0;
}
Output:
- A file named
student.txt
will be created with the following content:
Name: Pawan Jaiswal
Age: 21
Grade: A
2. Binary File Writing
Binary file writing involves writing data in binary format, which means the data is stored exactly as it exists in memory. This format is not human-readable, as it stores raw bytes of data. Binary file writing is particularly useful for handling large amounts of data, like images, audio files, or when working with arrays, structures, and other complex data types.
Key Characteristics:
- Data is stored in raw byte format.
- The file contents are not human-readable.
- Data is written and read as blocks or chunks.
- Suitable for storing large datasets, multimedia files (e.g., images, audio), or structured data (e.g., arrays and structures).
Real-Life Analogy:
A binary file is like a USB drive storing various files (music, videos, etc.). You can’t open the USB drive and read the contents directly, but software programs can read and write data to it.
Binary File Writing Functions in C
Here are the key functions used for writing to binary files in C:
fwrite()
: Used for writing data in blocks (binary format) to a file.fputc()
: Can also be used to write characters in binary mode.
Example of Writing to a Binary File Using fwrite()
:
#include <stdio.h>
int main() {
FILE *filePointer;
int numbers[] = {10, 20, 30, 40, 50}; // Array of integers
// Open file in binary write mode
filePointer = fopen("data.bin", "wb");
if (filePointer == NULL) {
printf("Error opening file!\n");
return 1;
}
// Writing the array to the binary file
fwrite(numbers, sizeof(int), 5, filePointer);
// Closing the file
fclose(filePointer);
printf("Binary data written to file successfully.\n");
return 0;
}
Output:
- A binary file named
data.bin
will be created, containing the raw binary data of the integer array{10, 20, 30, 40, 50}
. This file cannot be read by a text editor, but can be read back into the program usingfread()
.
Comparison Between Text and Binary File Writing
Aspect | Text File Writing | Binary File Writing |
---|---|---|
File Format | Human-readable (ASCII or UTF-8) | Non-human-readable (Raw binary data) |
Usage | Used for storing plain text, logs, reports, etc. | Used for storing raw data, arrays, structures, multimedia files (images, audio, etc.) |
Functions Used | fprintf() , fputs() , fputc() | fwrite() , fputc() |
File Mode | "w" , "a" | "wb" , "ab" |
Data Accessibility | Can be opened and edited using any text editor | Needs a specific program to read and process the binary data |
Efficiency | May add extra formatting characters (like newlines) | Stores raw data directly, making it faster and more memory-efficient |
Real-Life Examples
Text File Writing Example (Logs/Reports):
Imagine you’re writing a program to log user activity on a website. You could use a text file to store each user’s action in a readable format, such as:
User: Pooja Jaiswal, Action: Logged In
User: Pawan Jaiswal, Action: Logged Out
Binary File Writing Example (Image Storage):
Suppose you’re developing a program to handle image data. The raw pixel values of an image would be stored in a binary file since it’s more efficient and requires less space than storing the pixel values in a text file.
Best Practices for File Writing in C
- Always Close the File: Always close the file using
fclose()
after writing to ensure the data is saved properly and system resources are released. - Check for NULL File Pointers: Before performing any file operations, always check if
fopen()
returns NULL to avoid crashes or data loss. - Use Binary Writing for Efficiency: When working with large amounts of data, or when performance is critical (e.g., image or video processing), prefer binary file writing (
fwrite()
). - Choose the Correct Mode: Use the correct mode for the file operations (
"w"
,"a"
,"wb"
, etc.) depending on whether you’re writing in text or binary mode, and whether you want to append or overwrite the file.
Understanding the differences between text file writing and binary file writing is essential for choosing the right method based on your specific needs. While text files are ideal for human-readable data, binary files offer efficiency and precision when working with raw data. At SamagraCS Educational Technology, we encourage students to practice both types of file writing to become proficient in managing file I/O operations.
For any questions or further assistance, feel free to reach out to Pawan & Pooja, or the team at SamagraCS Educational Technology. Keep practicing and happy coding!