Binary File I/O in C
Binary file input and output (I/O) is an essential concept in C programming that allows you to read and write binary data to and from files. Binary file I/O provides a way to store and retrieve complex data structures, such as arrays and structures, without losing their original format. In this post, we'll explore the basics of binary file I/O in C, along with simple examples for better understanding.
Table of Contents
- Introduction to Binary File I/O
- Opening Binary Files
- Writing Binary Data
- Reading Binary Data
Example Programs
- Conclusion
Introduction to Binary File I/O
Binary file I/O involves working with files in their raw binary format, which is different from text file I/O. In binary file I/O, data is read and written as sequences of bytes, preserving the exact data representation. This makes binary file I/O suitable for storing and transferring structured data.
Opening Binary Files
Before performing any file I/O operations, we need to open the file. The fopen() function is used for this purpose. To open a file in binary mode, you can use the "rb" mode for reading and "wb" mode for writing. Let's see an example:
#include <stdio.h>
int main() {
FILE *file;
file = fopen("data.dat", "wb");
if (file == NULL) {
printf("Error opening file!\\n");
return 1;
}
// Perform file operations...
fclose(file);
return 0;
}
Writing Binary Data
To write binary data to a file, we can use the fwrite() function. It takes four arguments: a pointer to the data, size of each element, number of elements, and the file pointer. Here's an example of writing an array to a binary file:
#include <stdio.h>
int main() {
FILE *file;
file = fopen("data.dat", "wb");
if (file == NULL) {
printf("Error opening file!\\n");
return 1;
}
int data[] = {10, 20, 30, 40, 50};
int num_elements = sizeof(data) / sizeof(data[0]);
fwrite(data, sizeof(int), num_elements, file);
fclose(file);
return 0;
}
Reading Binary Data
To read binary data from a file, we can use the fread() function. It reads a specified number of elements of a given size from the file. Here's an example of reading data from a binary file:
#include <stdio.h>
int main() {
FILE *file;
file = fopen("data.dat", "rb");
if (file == NULL) {
printf("Error opening file!\\n");
return 1;
}
int data[5];
int num_elements = sizeof(data) / sizeof(data[0]);
fread(data, sizeof(int), num_elements, file);
for (int i = 0; i < num_elements; i++) {
printf("%d ", data[i]);
}
fclose(file);
return 0;
}
Example Programs
Writing Data to a Binary File
Let's consider an example where we write student records to a binary file:
#include <stdio.h>
struct Student {
char name[50];
int age;
};
int main() {
FILE *file;
file = fopen("students.dat", "wb");
if (file == NULL) {
printf("Error opening file!\\n");
return 1;
}
struct Student students[] = {
{"Alice", 20},
{"Bob", 21},
{"Carol", 19}
};
int num_students = sizeof(students) / sizeof(students[0]);
fwrite(students, sizeof(struct Student), num_students, file);
fclose(file);
return 0;
}
Reading Data from a Binary File
Now, let's read and display the student records from the binary file:
#include <stdio.h>
struct Student {
char name[50];
int age;
};
int main() {
FILE *file;
file = fopen("students.dat", "rb");
if (file == NULL) {
printf("Error opening file!\\n");
return 1;
}
struct Student students[3];
int num_students = sizeof(students) / sizeof(students[0]);
fread(students, sizeof(struct Student), num_students, file);
for (int i = 0; i < num_students; i++) {
printf("Name: %s, Age: %d\\n", students[i].name, students[i].age);
}
fclose(file);
return 0;
}
Conclusion
Binary file I/O is a powerful feature of C programming that allows you to work with complex data in its original format. By using fwrite() and fread(), you can easily write and read binary data from files. This enables you to store and retrieve structured data efficiently, making it an essential skill for any C programmer.
In this post, we covered the basics of binary file I/O, including opening files in binary mode, writing data to binary files, and reading data from binary files. We also provided simple example programs to help you grasp the concepts.
Feel free to experiment further with binary file I/O to enhance your understanding and proficiency in C programming.
0 Comments