Reading and writing binary file in C++
This tutorial gives the description as well as the steps for programmers to learn how reading and writing binary file in C++ works. The process is simple and is divided into two main parts, writing the file first and then reading the same.
Reading and writing binary file in C++
The tutorial consists of two main parts. We will first see how to write to a binary file and then see how to read from it.
1. Libraries
Code :
#include<iostream> #include<fstream>
iostream: input and output functions
stream : file stream.
2. Structure to store data to be written
Code :
struct Student { int roll_no; string name; }; int main() { Student write_stu[2]; write_stu[0].roll_no = 1; write_stu[0].name = "Akash"; write_stu[1].roll_no = 2; write_stu[1].name = "Arjun"; return 0; }
The data for a student database is created and will be used to write to the binary file.
3. Writing to the Binary file
Code :
ofstream wf("student.dat", ios::out | ios::binary); if(!wf) { cout << "Cannot open file!" << endl; return 1; } for(int i = 0; i < 2; i++) wf.write((char *) &write_stu[i], sizeof(Student)); wf.close(); if(!wf.good()) { cout << "Error occurred at writing time!" << endl; return 1; }
ofstream wf: Creates Output stream class object (here ‘wf’) to operate on files.
wf(“student.dat”, ios::out | ios::binary);
student.dat: file name
ios::out: allows output (write operations) to a stream
ios::binary: Open in binary mode
if wf does not return a pointer, the user is informed that the file cannot be opened.
if the pointer is successfully returned, the data is written using write function.
wf.write((char *) &write_stu[i], sizeof(Student));
(char *) &write_stu[i]: Refers to the data to be written
sizeof(Student): The size of data to be written at a time.
The loop is run for the required times of iterations to write the complete data to the file.
wf.close(): Used to close the file
wf.good(): Used to determine if the process was successful or if there was any error and if so, the error occurrence is informed to the user.
(Note : Whenever any error occurs as mentioned above, the code process is stopped.)
4. Reading from a binary file
Code :
Student read_stu[2]; ifstream rf("student.dat", ios::out | ios::binary); if(!rf) { cout << "Cannot open file!" << endl; return 1; } for(int i = 0; i < 2; i++) rf.read((char *) &read_stu[i], sizeof(Student)); rf.close(); if(!rf.good()) { cout << "Error occurred at reading time!" << endl; return 1; }
Another student class object array ‘read_stu’ is declared to store the read data.
ifstream rf : Creates Input stream class object (here ‘rf’) to operate on files.
rf(“student.dat”, ios::out | ios::binary);
student.dat : file name
ios::out : allows output (write operations) to a stream
ios::binary : Open in binary mode
if rf does not return a pointer, the user is informed that the file cannot be opened.
if the pointer is successfully returned, the data is read using read
function.
rf.read((char *) &read_stu[i], sizeof(Student));
(char *) &write_stu[i] : Refers to the data to be read
sizeof(Student) : The size of data to be read at a time.
The loop is run for the required times of iterations to read the complete data from the file.
rf.close() : Used to close the file
rf.good() : Used to determine if the process was successful or if there was any error and if so, the error occurrence is informed to the user.
(Note : Whenever any error occurs as mentioned above, the code process is stopped.)
5. Display
Code :
cout<<"Student Details:"<<endl; for(int i=0; i < 2; i++) { cout << "Roll No: " << read_stu[i].roll_no << endl; cout << "Name: " << read_stu[i].name << endl; cout << endl; }
The data which is read from the file and stored in the read_stu array is displayed.
Output :
Student Details: Roll No: 1 Name: Akash Roll No: 2 Name: Arjun
Complete Code
#include<iostream> #include<fstream> using namespace std; struct Student { int roll_no; string name; }; int main() { Student write_stu[2]; write_stu[0].roll_no = 1; write_stu[0].name = "Akash"; write_stu[1].roll_no = 2; write_stu[1].name = "Arjun"; ofstream wf("student.dat", ios::out | ios::binary); if(!wf) { cout << "Cannot open file!" << endl; return 1; } for(int i = 0; i < 2; i++) wf.write((char *) &write_stu[i], sizeof(Student)); wf.close(); if(!wf.good()) { cout << "Error occurred at writing time!" << endl; return 1; } Student read_stu[2]; ifstream rf("student.dat", ios::out | ios::binary); if(!rf) { cout << "Cannot open file!" << endl; return 1; } for(int i = 0; i < 2; i++) rf.read((char *) &read_stu[i], sizeof(Student)); rf.close(); if(!rf.good()) { cout << "Error occurred at reading time!" << endl; return 1; } cout<<"Student Details:"<<endl; for(int i=0; i < 2; i++) { cout << "Roll No: " << read_stu[i].roll_no << endl; cout << "Name: " << read_stu[i].name << endl; cout << endl; } return 0; }
Output :
Student Details: Roll No: 1 Name: Akash Roll No: 2 Name: Arjun
Therefore, we have learned how to read and write a binary file in C++.
You may also learn,
Leave a Reply