#include <iostream>
#include <fstream>
using namespace std;
struct Student {
int id;
char name[50];
float marks;
};
int main() {
// Write binary data
ofstream outFile("students.dat", ios::binary);
if (outFile.is_open()) {
Student s1 = {101, "Alice", 95.5};
Student s2 = {102, "Bob", 87.0};
Student s3 = {103, "Charlie", 92.5};
// Write structures to binary file
outFile.write((char*)&s1, sizeof(Student));
outFile.write((char*)&s2, sizeof(Student));
outFile.write((char*)&s3, sizeof(Student));
outFile.close();
cout << "Binary data written successfully!" << endl;
}
// Read binary data
ifstream inFile("students.dat", ios::binary);
if (inFile.is_open()) {
Student s;
cout << "\nReading binary data:" << endl;
while (inFile.read((char*)&s, sizeof(Student))) {
cout << "ID: " << s.id << ", Name: " << s.name
<< ", Marks: " << s.marks << endl;
}
inFile.close();
}
return 0;
}Output
Binary data written successfully! Reading binary data: ID: 101, Name: Alice, Marks: 95.5 ID: 102, Name: Bob, Marks: 87 ID: 103, Name: Charlie, Marks: 92.5
This program teaches you how to perform Binary File Operations in C++. Binary files store data in raw byte format, making them more efficient for structured data like structures and arrays. Binary operations preserve exact data representation without text conversion.
1. What This Program Does
The program demonstrates binary file operations:
- Writing structures to binary file using write()
- Reading structures from binary file using read()
- Using ios::binary mode flag
- Preserving exact data representation
Binary files enable efficient storage of structured data.
2. Header Files Used
-
#include <iostream>
- Provides cout and cin for input/output operations.
-
#include <fstream>
- Provides file stream classes (ofstream, ifstream).
3. Understanding Binary Files
Binary File Concept:
- Stores data as raw bytes
- No text conversion
- Preserves exact representation
- More efficient for structured data
vs Text Files:
- Text: human-readable, larger size
- Binary: machine-readable, smaller size
- Binary: faster read/write
- Binary: exact data preservation
4. Opening Binary File
Using ios::binary:
ofstream outFile("students.dat", ios::binary); ifstream inFile("students.dat", ios::binary);
How it works:
- ios::binary: binary mode flag
- No text conversion
- Raw byte operations
- Efficient for structures
5. Writing Binary Data
Using write():
Student s1 = {101, "Alice", 95.5}; outFile.write((char*)&s1, sizeof(Student));
How it works:
- write(): writes raw bytes
- (char*)&s1: cast structure pointer to char*
- sizeof(Student): number of bytes to write
- Writes entire structure as bytes
Syntax:
write((char*)&object, sizeof(objectType))
6. Reading Binary Data
Using read():
Student s; inFile.read((char*)&s, sizeof(Student));
How it works:
- read(): reads raw bytes
- (char*)&s: cast structure pointer to char*
- sizeof(Student): number of bytes to read
- Reads entire structure from bytes
Loop Reading:
while (inFile.read((char*)&s, sizeof(Student))) { // Process each structure }
7. When to Use Binary Files
Best For:
- Structured data (structures, arrays)
- Large datasets
- Performance-critical I/O
- Exact data preservation
- Database-like storage
Example Scenarios:
- Student records
- Image data
- Database files
- Game save files
- Performance-critical applications
8. Important Considerations
Data Structure:
- Must match exactly when reading
- Same structure definition
- Same member order
- Same data types
Portability:
- Binary files may not be portable
- Different systems (endianness)
- Platform-dependent
- Use text for portability
Size Efficiency:
- Binary: smaller file size
- No text conversion overhead
- Faster read/write
- More efficient for large data
9. return 0;
This ends the program successfully.
Summary
- Binary files: use ios::binary mode flag, store data as raw bytes.
- write(): writes raw bytes, syntax: write((char*)&object, sizeof(type)).
- read(): reads raw bytes, syntax: read((char*)&object, sizeof(type)).
- More efficient for structured data, preserves exact representation.
- Understanding binary files enables efficient data storage.
- Essential for performance-critical applications and structured data storage.
This program is fundamental for learning binary I/O, understanding efficient data storage, and preparing for database-like file operations in C++ programs.