C++program to delete the content of a Binary File Last Updated : 11 Nov, 2019 Summarize Comments Improve Suggest changes Share Like Article Like 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: CPP // 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: to delete the record of the file C++ program to delete the content of a Binary File Comment More infoAdvertise with us Next Article Remove comments from a given C/C++ program V vinaychopra92vc Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads 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 4 min read Remove comments from a given C/C++ program Given a C/C++ program, remove comments from it. We strongly recommend to minimize your browser and try this yourself first. The idea is to maintain two flag variables, one to indicate that a single line comment is started, another to indicate that a multiline comment is started. When a flag is set, 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 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 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 C++ Program that will fill whole memory NOTE:We strongly recommend to try this code in virtual machine because it may hang your computer within 5 second Dynamic memory allocation in C/C++ refers to performing memory allocation manually by programmer. Dynamically allocated memory is allocated on Heap and non-static and local variables get 2 min read Like