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”.
Input: roll no: 1 Output The deleted record is roll no: 1 name: vinay record successfully deleted Input: roll no: 2 Output: record not found
In this example, the existing roll number of the student whose record is to be deleted is taken from the user and we will create a new file in which we will write all the records of the first file except the record to be deleted and then delete the first file and rename new file with the name of the first file
Below are the various steps on how to do so:
- Step 1:Opening the file from which the record is to be deleted in reading mode here “he.dat”
- Step 2: Opening the file to which the new content is to be written in writing mode here “temp.dat”
- Step 3:Reading the file and Comparing the record roll no with that to be deleted
- Step 4: If during reading the roll number to be deleted exists then display it else write the record in temp.dat
- Step 5: Finally after reading the complete file delete the file “he.dat” and rename the new file “temp.dat” with “he.dat”
- Step 6: If in the file record exist then print the record and print record successfully deleted
- Step 7: If the roll number does not exists then print “record not found”.
Standard Library Functions used:
// to remove the file remove("name_of_file"); // to rename file1 as file2 rename("name-of_file1", "name_of_file2");
Below is the implementation of the above approach:
- C++ program to modify the content of a Binary File
- Reverse the content of a file and store it in another
- C++ program to append content of one text file to another
- Program to delete a line given the line number from a file
- delete() in C++
- How to delete last element from a set in C++
- Program to delete Nth digit of a Number
- How to delete a range of values from the Set using Iterator
- Delete all Non-Prime Nodes from a Singly Linked List
- CSV file management using C++
- C++ program to create a file
- Read a record from a File in C++ using seekg() and tellg()
- C++ program to read file word by word
- Check if all the 1's in a binary string are equidistant or not
- Zig-Zag traversal of a Binary Tree using Recursion
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.