
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reading and Writing Binary File in C/C++
Writing a Binary File
To write a binary file in C/C++ use fwrite()/write() method. It is used to write a given number of bytes on the given stream, starting at the position of the "put" pointer. The file is extended if the put pointer is currently at the end of the file. If this pointer points into the middle of the file, characters in the file are overwritten with the new data.
If any error has occurred during writing in the file, the stream is placed in an error state.
Syntax of write() method
Following is the basic syntax of writing a binary file:
ostream& write(const char*, int);
Reading a Binary File
To read a binary file in C/C++ use fread()/read() method. It extracts a given number of bytes from the given stream and place them into the memory, pointed to the first parameter. If any error is occurred during reading in the file, the stream is placed in an error state, all future read operation will be failed then.
gcount() can be used to count the number of characters has already read. Then clear() can be used to reset the stream to a usable state.
Syntax of read() method
Following is the syntax to read the binary file:
ifstream& write(const char*, int);
Algorithm
Following is the list of steps to read and write binary file in C/C++:
- Begin
- Create a structure Student to declare variables.
- Open a binary file for writing.
- Check if any error occurs in file opening.
- Initialize the variables with data.
- If the file opens successfully, write the binary data using the write method.
- Close the file after writing.
- Open the binary file for reading.
- Check if any error occurs in file opening.
- If the file opens successfully, read the binary data using the read method.
- Close the file after reading.
- Check if any error occurs.
- Print the data.
- End
Code for Reading and Writing Binary File
In this example, we perform file handling functions like fread()/read() and write ()/write() to read and write binary files in C/C++:
#include<stdio.h> #include<stdlib.h> #include<string.h> // Define the structure to store student details struct Student { int roll_no; char name[50]; }; int main() { // Open the binary file for writing FILE *wf = fopen("student.dat", "wb"); // Check if file open successfully if (!wf) { printf("Cannot open file!\n"); return 1; } // Initialize student data struct Student wstu[3]; wstu[0].roll_no = 1; strcpy(wstu[0].name, "Ram"); wstu[1].roll_no = 2; strcpy(wstu[1].name, "Shyam"); wstu[2].roll_no = 3; strcpy(wstu[2].name, "Madhu"); // Write student data to the file for (int i = 0; i < 3; i++) fwrite(&wstu[i], sizeof(struct Student), 1, wf); // Close the file after writing fclose(wf); // Open the binary file for reading wf = fopen("student.dat", "rb"); // Check if file open sucessfully if (!wf) { printf("Cannot open file!\n"); return 1; } // Read student data from the file struct Student rstu[3]; for (int i = 0; i < 3; i++) fread(&rstu[i], sizeof(struct Student), 1, wf); // Close the file after reading fclose(wf); // Display student details printf("Students Details:\n"); for (int i = 0; i < 3; i++) { printf("Roll No: %d\n", rstu[i].roll_no); printf("Name: %s\n", rstu[i].name); printf("\n"); } return 0; }
Output
The above program produces the following result:
Students Details: Roll No: 1 Name: Ram Roll No: 2 Name: Shyam Roll No: 3 Name: Madhu
#include <iostream> #include <fstream> using namespace std; // Define the structure to store student details struct Student { int roll_no; string name; }; int main() { // Open a binary file for writing ofstream wf("student.dat", ios::out | ios::binary); // Check if file open successfully if (!wf) { cout << "Cannot open file!" << endl; return 1; } // Initialize student data Student wstu[3]; wstu[0].roll_no = 1; wstu[0].name = "Ram"; wstu[1].roll_no = 2; wstu[1].name = "Shyam"; wstu[2].roll_no = 3; wstu[2].name = "Madhu"; // Write student data to the file for (int i = 0; i < 3; i++) wf.write((char*)&wstu[i], sizeof(Student)); // Close the file after writing wf.close(); // Check if writing was successful if (!wf.good()) { cout << "Error occurred at writing time!" << endl; return 1; } // Open the binary file for reading ifstream rf("student.dat", ios::in | ios::binary); // Check if file open successfully if (!rf) { cout << "Cannot open file!" << endl; return 1; } // Read student data from the file Student rstu[3]; for (int i = 0; i < 3; i++) rf.read((char*)&rstu[i], sizeof(Student)); // Close the file after reading rf.close(); // Check if reading was successful if (!rf.good()) { cout << "Error occurred at reading time!" << endl; return 1; } // Display student details cout << "Students Details:" << endl; for (int i = 0; i < 3; i++) { cout << "Roll No: " << rstu[i].roll_no << endl; cout << "Name: " << rstu[i].name << endl; cout << endl; } return 0; }
Output
The above program produces the following result:
Students Details: Roll No: 1 Name: Ram Roll No: 2 Name: Shyam Roll No: 3 Name: Madhu