Binary File Operations in C++

Binary File Reading and Writing in C++.

IntermediateFile Handling ProgramsExample 4 of 6
binary-file-operations.cpp
1#include <iostream>
2#include <fstream>
3using namespace std;
4
5struct Student {
6 int id;
7 char name[50];
8 float marks;
9};
10
11int main() {
12 // Write binary data
13 ofstream outFile("students.dat", ios::binary);
14
15 if (outFile.is_open()) {
16 Student s1 = {101, "Alice", 95.5};
17 Student s2 = {102, "Bob", 87.0};
18 Student s3 = {103, "Charlie", 92.5};
19
20 // Write structures to binary file
21 outFile.write((char*)&s1, sizeof(Student));
22 outFile.write((char*)&s2, sizeof(Student));
23 outFile.write((char*)&s3, sizeof(Student));
24
25 outFile.close();
26 cout << "Binary data written successfully!" << endl;
27 }
28
29 // Read binary data
30 ifstream inFile("students.dat", ios::binary);
31
32 if (inFile.is_open()) {
33 Student s;
34 cout << "\nReading binary data:" << endl;
35
36 while (inFile.read((char*)&s, sizeof(Student))) {
37 cout << "ID: " << s.id << ", Name: " << s.name
38 << ", Marks: " << s.marks << endl;
39 }
40
41 inFile.close();
42 }
43
44 return 0;
45}

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

What's going on

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.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <fstream>
Provides file stream classes (ofstream, ifstream).

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

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

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))

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))) {

}

1// Process each structure

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

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

return 0;

This ends the program successfully.