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”.
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.
- 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:
- C++program to delete the content of a Binary File
- C++ program to append content of one text file to another
- Reverse the content of a file and store it in another
- C++ program to create a file
- Program to delete a line given the line number from a file
- C++ program to read file word by word
- Recursive Program for Binary to Decimal
- CSV file management using C++
- Zig-Zag traversal of a Binary Tree using Recursion
- Count the number of 1's and 0's in a binary array using STL in C++ ?
- Check if all the 1's in a binary string are equidistant or not
- Flatten Binary Tree in order of Zig Zag traversal
- Clockwise Spiral Traversal of Binary Tree | Set - 2
- Occurrences of a pattern in binary representation of a number
- Clockwise Spiral Traversal of Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.