The Wayback Machine - https://web.archive.org/web/20240720070942/https://www.geeksforgeeks.org/cprogram-to-delete-the-content-of-a-binary-file/
Open In App

C++program to delete the content of a Binary File

Last Updated : 11 Nov, 2019
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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 deleted record is 
roll no: 1
name: vinay
record successfully deleted

Input:
roll no: 2
Output:
record not found 

Approach:
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 delete the record
// of a binary file
  
#include <bits/stdc++.h>
  
using namespace std;
  
class abc {
    int roll;
    char name[20];
  
public:
    void deleteit(int);
    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 delete
// the content of the binary file
void abc::deleteit(int rno)
{
  
    int pos, flag = 0;
  
    ifstream ifs;
    ifs.open("he.dat", ios::in | ios::binary);
  
    ofstream ofs;
    ofs.open("temp.dat", ios::out | ios::binary);
  
    while (!ifs.eof()) {
  
        ifs.read((char*)this, sizeof(abc));
  
        // if(ifs)checks the buffer record in the file
        if (ifs) {
  
            // comparing the roll no with
            // roll no of record to be deleted
            if (rno == roll) {
                flag = 1;
                cout << "The deleted record is \n";
  
                // display the record
                putdata();
            }
            else {
                // copy the record of "he" file to "temp" file
                ofs.write((char*)this, sizeof(abc));
            }
        }
    }
  
    ofs.close();
    ifs.close();
  
    // delete the old file
    remove("he.dat");
  
    // rename new file to the older file
    rename("temp.dat", "he.dat");
  
    if (flag == 1)
        cout << "\nrecord successfully deleted \n";
    else
        cout << "\nrecord not found \n";
}
  
// Sample input 1
void abc::testcase1()
{
    int rno;
  
    // roll no to be searched
    rno = 1;
  
    // call deleteit function
    // with the roll no. of record to be deleted
    deleteit(rno);
}
  
// Sample input 2
void abc::testcase2()
{
    int rno;
  
    // roll no to be searched
    rno = 4;
  
    // call deleteit function
    // with the roll no of record to be deleted
    deleteit(rno);
}
  
// Driver code
int main()
{
    abc s;
  
    // sample case 1
    s.testcase1();
  
    // sample case 2
  
    s.testcase2();
  
    return 0;
}


Output:

delete record

to delete the record of the file



Similar Reads

C++ Program to Read Content From One File and Write it Into Another File
Here, we will see how to read contents from one file and write it to another file using a C++ program. Let us consider two files file1.txt and file2.txt. We are going to read the content of file.txt and write it in file2.txt Contents of file1.txt: Welcome to GeeksForGeeks Approach: Create an input file stream object and open file.txt in it.Create a
2 min read
C++ program to modify the content of a Binary File
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
4 min read
Program to delete a line given the line number from a file
Given a file and a line number n, the task is to delete nth line from the given text file. Suppose the initial content of myfile.txt is : GeeksforGeeks GeeksforGeeks IDE GeeksforGeeks Practice GeeksforGeeks Contribute After Deletion of Line 2 the content would be : GeeksforGeeks GeeksforGeeks IDE GeeksforGeeks Contribute Approach : 1) Open the sour
2 min read
C++ program to append content of one text file to another
Given source and destination text files. Append the content from the source file to the destination file and then display the content of the destination file.Example : Input : file.txt : "geeks", file2.txt : "geeks for" Output: file2.txt : "geeks for geeks" Method 1:Approach: Open file.txt in inputstream and file2.txt in outputstream with the appen
3 min read
How to create Binary File from the existing Text File?
In this article, we will discuss how to create a binary file from the given text file. Before proceeding to the steps, let's have an introduction of what are text files and binary files. Text files: Text files store data in human-readable form and sequentially. Binary files: Binary files store data in the form of bits/groups of bits (bytes) in sequ
7 min read
How to Delete a File in C++?
C++ file handling allows us to manipulate external files from our C++ program. We can create, remove, and update files using file handling. In this article, we will learn how to remove a file in C++. Delete a File in C++ To remove a file in C++, we can use the remove() function defined inside the &lt;stdio.h&gt; header file. The remove() function t
2 min read
Reverse the content of a file and store it in another
The article explains how to store the reverse of the first text file's data to a second text file. In this post, it has been assumed that there is no text in the first text file. So we will write some text in a first text file by passing data to reverse function and then in reverse function we will copy the reverse of its data to another text file.
3 min read
C++ Program to Copy One File into Another File
To copy the text/contents of one file to another file, we should know the basics of reading and writing a text file in C++. To copy the file using C++, we read the contents of the source file and write it into the destination file. Steps to copy one file to another in C++: Create objects of ifstream and ofstream classes.Check if they are connected
2 min read
C++ Program to Copy the Contents of One File Into Another File
Here, we will see how to develop a C++ program to copy the contents of one file into another file. Given a text file, extract contents from it and copy the contents into another new file. After this, display the contents of the new file. Approach:Open the first file which contains data. For example, a file named "file1.txt" contains three strings o
2 min read
Program to delete Nth digit of a Number
Given a number num and a number n, the task is to delete this nth digit of the number num, from starting and from end.Examples: Input: num = 1234, n = 3 Output: num_after_deleting_from_starting = 124, num_after_deleting_from_end = 134Input: num = 4516312, n = 2 Output: num_after_deleting_from_starting = 416312, num_after_deleting_from_end = 451632
15+ min read
Article Tags :
three90RightbarBannerImg