Working with Binary Files in C++
Last Updated on: 23rd Dec 2025 15:52:14 PM
In many real-world software applications, data is not always stored in human-readable text format. Applications such as banking systems, payroll software, student management systems, game engines, and operating systems often store data in binary format for better performance, security, and efficient storage.
Binary files store data in the same format as it is stored in memory, which makes file operations faster and more compact compared to text files. Unlike text files, binary files do not translate data into characters; instead, they store raw bytes exactly as they are.
In C++, working with binary files is an important skill for developers who want to build professional, high-performance applications. Understanding binary file handling helps programmers efficiently store objects, structures, and large datasets while maintaining data integrity. This tutorial explains binary file handling in C++ in a simple, step-by-step, beginner-friendly manner, with clear examples and real-life use cases.
What is a Binary File?
A binary file is a file that stores data in binary (byte) format rather than readable text. The data stored in a binary file cannot be directly read or understood by humans using a normal text editor.
Binary files are mainly used when:
-
Data size is large
-
Data must be stored exactly as it is
-
Faster read and write operations are required
Real-Life Example of Binary File Handling
Consider a banking application that stores customer details such as account number, balance, and PIN. Storing this data in text format would be slower and less secure. Instead, the application stores customer records in a binary file, which allows faster access and prevents easy tampering.
Binary File vs Text File
| Text File | Binary File |
|---|---|
| Human-readable | Not human-readable |
| Slower processing | Faster processing |
| Larger file size | Smaller file size |
| Stored as characters | Stored as raw bytes |
File Stream Used for Binary Files
Binary files in C++ are handled using the fstream , ifstream , or ofstream classes along with the ios::binary mode.
The two important functions used are:
-
write() for writing data to a binary file
-
read() for reading data from a binary file
Writing Data to a Binary File
Writing data to a binary file means storing data in its raw memory form. This is done using the write() function, which writes a specified number of bytes to the file.
Example
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int number = 100;
ofstream file("data.bin", ios::binary);
file.write((char*)&number, sizeof(number));
file.close();
return 0;
}
Output
Binary data written successfully
Explanation of Output
The program stores the integer value in binary format inside the file, and the file is closed properly to ensure the data is saved.
Reading Data from a Binary File
Reading from a binary file means retrieving raw data from the file and loading it back into memory using the read() function.
Example
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int number;
ifstream file("data.bin", ios::binary);
file.read((char*)&number, sizeof(number));
file.close();
cout << "Number read from file: " << number << endl;
return 0;
}
Output
Number read from file: 100
Explanation of Output
The program reads the binary data from the file and correctly reconstructs the integer value stored earlier.
Writing an Object to a Binary File
Binary files are commonly used to store objects or structures directly. This allows entire records to be saved and retrieved efficiently.
Example
#include <iostream>
#include <fstream>
using namespace std;
class Student {
public:
int roll;
char name[20];
};
int main() {
Student s = {101, "Amit"};
ofstream file("student.bin", ios::binary);
file.write((char*)&s, sizeof(s));
file.close();
return 0;
}
Output
Student record written to binary file
Explanation of Output
The program stores the entire student object in binary format inside the file, preserving the exact memory structure.
Reading an Object from a Binary File
Reading an object from a binary file restores the stored data back into the program exactly as it was written.
Example
#include <iostream>
#include <fstream>
using namespace std;
class Student {
public:
int roll;
char name[20];
};
int main() {
Student s;
ifstream file("student.bin", ios::binary);
file.read((char*)&s, sizeof(s));
file.close();
cout << "Roll: " << s.roll << endl;
cout << "Name: " << s.name << endl;
return 0;
}
Output
Roll: 101
Name: Amit
Explanation of Output
The program reads the binary data from the file and displays the student details exactly as they were stored.
Appending Data to a Binary File
Appending data allows new records to be added to an existing binary file without removing previous records.
Example
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int value = 200;
ofstream file("data.bin", ios::binary | ios::app);
file.write((char*)&value, sizeof(value));
file.close();
return 0;
}
Output
Binary data appended successfully
Explanation of Output
The program adds new binary data at the end of the file while keeping the existing data intact.
Advantages of Binary File Handling
Binary file handling provides faster read and write operations, reduces file size, improves performance, and allows direct storage of objects and structures.
Disadvantages of Binary File Handling
Binary files are not human-readable, may face portability issues across different systems, and require careful handling to avoid data corruption.
Real-World Applications of Binary Files
Binary files are widely used in banking software, payroll systems, game development, database storage, operating systems, and embedded systems where performance and data integrity are critical.
Conclusion
Working with binary files in C++ is an essential skill for developing efficient and high-performance applications. Binary file handling allows data to be stored and retrieved quickly in its raw form, making it ideal for storing objects and large datasets. By understanding how to use binary file streams, read() and write() functions, and proper file modes, programmers can build secure, scalable, and professional software solutions.
Keep practicing — you're doing amazing!
Happy Coding! ![]()