C++ program to modify the content of a Binary File Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report This article explains how to modify the content of a Binary File. Given a binary file that contains the records of students, the task is to modify or alter the record of the specified student. If the record of no such student exists, then print "record not found". Examples: Input: old roll no: 1 new roll no: 11 new name: "Geek" Output: roll no: 11 name: "Geek" record successfully modified Input: old roll no: 234 new roll no: 14 new name: "Geek2" Output: record not found In this example, the existing roll number of the student whose record is to be modified is taken from the user and a newly updated record is created that replaces the previous record. Approach: Step 1: Searching for the roll number in the binary file. Step 2: While searching in the file, the variable "pos" stores the position of file pointer record then traverse(continue) reading of the record. Step 3: If the roll number to be searched exists then place the write pointer (to ending of the previous record) i.e. at pos. Step 4: Call getdata function to take the new record. Step 5: Write the new object at the position "pos" and hence the record is updated and print "record successfully updated". Step 6: If the roll number does not exists then print "record not found". Standard Library Functions used: // tells the position of read pointer file.tellg(); // places the writing pointer at // position "pos" in the file file.seekp(pos); Below is the implementation of the above approach: CPP // C++ program to modify the content // of a binary file #include <bits/stdc++.h> using namespace std; class abc { int roll; char name[20]; public: void getdata(int, char[]); void update(int, int, char[]); void testcase1(); void testcase2(); void putdata(); }; // Code to display the data of the // data of the object void abc::putdata() { cout << "roll no: "; cout << roll; cout << "\nname: "; cout << name; } // Code to set the value to the object void abc::getdata(int a, char str[]) { // setting the new roll no roll = a; // setting new name strcpy(name, str); } void abc::update(int rno, int r, char str[]) { // code to update and modify // the content of the binary file int pos, flag = 0; // rno=9 fstream fs; fs.open("he.dat", ios::in | ios::binary | ios::out); while (!fs.eof()) { // storing the position of // current file pointeri.e. at // the end of previously read record pos = fs.tellg(); fs.read((char*)this, sizeof(abc)); if (fs) { // comparing the roll no with that // of the entered roll number if (rno == roll) { flag = 1; // setting the new (modified ) // data of the object or new record getdata(r, str); // placing the put(writing) pointer // at the starting of the record fs.seekp(pos); // writing the object to the file fs.write((char*)this, sizeof(abc)); // display the data putdata(); break; } } } fs.close(); if (flag == 1) cout << "\nrecord successfully modified \n"; else cout << "\nrecord not found \n"; } // Sample input 1 void abc::testcase1() { int rno, r; char name[20]; // roll no to be searched rno = 1; // new roll no r = 11; // new name strcpy(name, "Geek"); // call update function with new values update(rno, r, name); } // Sample input 2 void abc::testcase2() { int rno, r; char name[20]; // roll no to be searched rno = 4; // new roll no r = 14; // new name strcpy(name, "Geek2"); // call update function with the new values update(rno, r, name); } // Driver code int main() { abc s; // sample case 1 s.testcase1(); // sample case 2 s.testcase2(); return 0; } Output: Comment More infoAdvertise with us Next Article Output of C++ programs | Set 34 (File Handling) V vinaychopra92vc Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads C++program to delete the content of a Binary File This article explains how to delete the content of a Binary File. Given a binary file that contains the records of students, the task is to delete the record of the specified student. If the record of no such student exists, then print ârecord not foundâ. Examples: Input: roll no: 1 Output The delet 3 min read Output of C++ programs | Set 34 (File Handling) What task does the following program perform? CPP #include<iostream> #include<fstream> using namespace std; int main() { ofstream ofile; ofile.open ("text.txt"); ofile << "geeksforgeeks" << endl; cout << "Data written to file" << endl 3 min read Difference Between C++ Text File and Binary File A text file is the one in which data is stored in the form of ASCII characters and is normally used for storing a stream of characters. Text files are organized around lines, each of which ends with a newline character ('\n'). The source code files are themselves text files. A binary file is the one 4 min read How To Compile And Run a C/C++ Code In Linux C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance. In this article, we will be compiling and exe 4 min read Four File Handling Hacks which every C/C++ Programmer should know We will discuss about four file hacks listed as below- Rename - Rename a file using C/C++Remove â Remove a file using C/C++File Size - Get the file size using C/C++Check existence â Check whether a file exists or not in C/C++ C++ // C++ Program to demonstrate the // four file hacks every C/C++ must 3 min read lseek() in C/C++ to read the alternate nth byte and write it in another file From a given file (e.g. input.txt) read the alternate nth byte and write it on another file with the help of âlseekâ. lseek (C System Call): lseek is a system call that is used to change the location of the read/write pointer of a file descriptor. The location can be set either in absolute or relati 2 min read Like