ap87
ap87

Reputation: 47

Deleting a record from a file in c++

I have wriiten a code to add , delete and dispaly a record of employees consisting of employee ID ,name,age and location. But I am unable to code the delete function

My code is as follows:

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;


class Document
{

public: 
int Add();
void Delete();
void Display();
int No_of_lines();
int empno();

private:
fstream document;
fstream newdocument;
string data;
int age;
int emp_id;
int idtodelete;
char name[100],loc[100];

};

int Document::No_of_lines()
{
int number = 0;
document.open("document.txt");
while (getline(document, data))
{
    ++number;
}
return number;
}


int Document::Add()
{
        Document d1;
        document.open ( "document.txt",ios::app);
        int x = d1.No_of_lines();
        int emp_id = ++x;
        cout << "The employee ID is: " << emp_id;
        document <<emp_id;
        cout<< "\n Ënter Name:" ;
        cin>>name;
        document <<"\t Name:"<< name;
        cout<<"Enter Age:";
        cin>> age;
        document << "\t Age:"<< age;
        cout<< "Enter location:";
        cin>> loc;
        document << "\t Location:"<< loc;
        document << "\n";
        document.close();           
        return 0;
}

void Document::Delete()
{
Document d2;
d2.Display();
int num;
char line[1000];
document.open("document.txt");
newdocument.open("newdocument.txt");
cout << "Enter the ID to delete \n";
cin >> idtodelete;
while (document.good())
{
    const int maxchar = 1000;
    const int maxtokens = 10;
    char* token[maxtokens] = {};
    char split[maxchar];
    document.getline(split, maxchar);
    int n = 0;
    token[0] = strtok(split, " ");
    istringstream(token[0]) >> num;
    if (num != idtodelete)
         {
             document >> emp_id >> name >> age >> loc;
             newdocument << emp_id<< name<< age<< loc;
         }
    else
    {
    }
}
    document.close();
    newdocument.close();
    remove("document.txt");
    rename("newdocument.txt", "document.txt");
}


void Document::Display()
{

        document.open("document.txt");

        while (!document.eof())
        {
            getline(document,data);
            cout<<data<<endl;
        }

        document.close();
}


int main()
{
Document d;
char ans;
int ch;

do
{
system ( "cls");
cout<< "Enter your choice \n";
cout << "\t1. Add Data \n " << "\t2. Delete Data \n" << "\t3. Display Data   \n";
cout<<  "\t4. Exit\n";
cout<< " Enter Choice \n ";
cin >> ch;

switch(ch)
{
    case 1:

        cout << " Adding Data : \n";
        d.Add();
        break;

    case 2:
        //cout << "Deleting data : \n"; 
        d.Delete();
        break;

    case 3:
        cout << "Displaying data : \n";
        d.Display();
        break;

    case 4:
        cout << "Exit";
        break;

    default :
        cout << "Invalid Input \n";
        break;
}
cout<< " click y to quit or any other key to continue " ;
cin>>ans;
}
while (ans != 'y');
return 0;
}

Upvotes: 0

Views: 7119

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149185

The simple way is to remove by employee ID. You just ask for the employee ID, to know what employee to remove.

Then, you cannot remove lines in the middle of a sequential file, so you just

  • rename the file as document.back
  • create a new document.txt
  • read document.back and copy all employees to document.txt except the one you want to delete
  • close both files
  • remove document.back

That's all ... except for the usual test for IO errors, backup file existing, and so on...

I tested your code. First, you forgot to close document in method int Document::No_of_lines(). Next on my MSVC2008, I have to explicitely call document.clear() after reaching end of file. You also do not test document immediately after a getline, meaning that you execute the code after a bad read.

I removed newdocument from Document class, because IMHO it is useless. Here is a possible implementation of Delete:

void Document::Delete()
{
    Document d2;
    Display();
    int num;
    document.open("document.txt");
    document.clear();
    d2.document.open("newdocument.txt", ios::out | ios::trunc);
    cout << "Enter the ID to delete \n";
    cin >> idtodelete;
    while (document.good())
    {
        getline(document, data);
        if (document) {
            int n = 0;
            istringstream(data) >> num;
            if (num != idtodelete)
            {
                d2.document << data << endl;
            }
        }
    }
    document.close();
    d2.document.close();
    remove("document.txt");
    rename("newdocument.txt", "document.txt");
}

Upvotes: 4

Related Questions