-1

How can I delete a record, on .txt file, For example I have records on .txt file:

Juan John Josh
Harry Potter Harry
Razor Paper Stone

The user will enter the name that he want to delete, Example: the user enter the name, Harry then the reocord will update and remove the record Harry Potter Harry.

Im asking for a code please. Please response.

2 Answers2

1
#include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    int main(int argc, char** arg) 
    {
        //declare testing this is the original text file variable
        ifstream testing;
        //open test.txt this is the original text file
        testing.open("test.txt");
        //decalare a temporary text file variable
        ofstream temp;
        //open a temporary text file
        temp.open("temp.txt");

        // user input to delete record
        cout<<"Enter Name to delete: ";
        //declaration of variables
        //variable for the input of the user
        string name;
        //variable for the record inside the original text file
        string name1;
        //reading input as the value of the variable name.
        cin>>name;

        //while this is for reading the original text file and the record will be the value for the variable name1
        while(testing>>name1)
        {
            //if variable name2(record) not equal to variable name(input)
            if(name2 != name)
            {
                //all the records in the original text file except to the record that the user inputted will be copied to the temporary text file which is temp.txt.
                temp<<name1<<" "<<name2<<" "<<name3<<" "<<grade1<<" "<<grade2<<" "<<grade3<<endl;
            }
        }

        //closing temp.txt
        testing.close();
        //closing original text file which is test.txt
        temp.close();
        //removing the original text file because it contains all the records including the record that was intended to be deleted.
        remove("test.txt");
        //renaming the temporary text file(temp.txt) to become the original text file(test.txt) 
        //note: that the temporary text file contains all the records except to the record the user deleted. 
        //and now renamed to be the original text file.
        rename("temp.txt", "test.txt");
        //the record is now deleted and its done hahaha
        return 0;
    }
0

It depends on the file format:

If all records (=lines) have same length (for example: with space padding) you can take the last record and write instead the record you want to delete, and trim the file to exclude the last record.

If it is plain text file, you will have to copy data, till the end of file like this:

  • read lines until you find the record you want to delete.
  • after you find the record, read next line and write over the previous.
SHR
  • 7,940
  • 9
  • 38
  • 57
  • 1
    A third option is to just tag the deleted record as deleted. If the name field is first, then perhaps a name of XXX means the record is deleted. It can be overwritten by a new record If the records are fixed size, or the new record is smaller and it can be padded. For privacy reasons you may want to make sure that all the data from the old record is obliterated, not just inject XXX in the first 3 characters. – Gem Taylor Dec 06 '18 at 11:08