I can't Delete a specific record from my program.I need Your Help Guys.Thank's in Advance..

#include<iostream>
#include<fstream>
using namespace std;
class teacher{
    long long cnicno;
    char name[30],fathername[30],address[50],grade[10];
public:
    void input()
    {
        system("cls");
        cout<<"Enter CNIC.No without '-' :  ";
        cin>>cnicno;
        cout<<"Enter Name               :  ";
        cin.ignore();
        cin.getline(name,30);
        cout<<"Enter Father Name        :  ";
        cin.getline(fathername,30);
        cout<<"Enter Address            :  ";
        cin.getline(address,50);
        cout<<"Enter Grade              :  ";
        cin.getline(grade,10);
    }
    void output()
    {
        cout<<"\n\n\n\n\n";
        cout<<"CNIC.No                  :  "<<cnicno<<endl;
        cout<<"Name                     :  "<<name<<endl;
        cout<<"Father Name              :  "<<fathername<<endl;
        cout<<"Address                  :  "<<address<<endl;
        cout<<"Grade                    :  "<<grade<<endl;
    }
    void modify()
    {
        system("cls");

        cout<<"\nRe-Enter Your New  Data ";

        cout<<"\nEnter New CNIC.No         :  ";
        cin>>cnicno;
        cout<<"\nEnter New Name           :  ";
        cin.ignore();
        cin.getline(name,30);
        cout<<"\nEnter New Father Name    :  ";
        cin.getline(fathername,30);
        cout<<"\nEnter New Address        :  ";
        cin.getline(address,50);
        cout<<"\nEnter New Grade          :  ";
        cin.getline(grade,10);
    }
    long long getcnicno()
    {
        return cnicno;
    }
};
void writedata()
{
    system("cls");
    ofstream outfile;
    teacher t1;

    outfile.open("teacherdata.txt",ios::out|ios::app);
    t1.input();
    outfile.write(reinterpret_cast<char*>(&t1),sizeof(teacher));
    cout<<"\nData Has Been Writen On File    ";
}
void readdata()
{
    system("cls");
    ifstream infile;
    teacher t1;

    infile.open("teacherdata.txt",ios::in);
    if(!infile)
    {
        cout<<"No File Found Plz First Enter Data  ";
    }
    else
    {
        infile.read(reinterpret_cast<char*>(&t1),sizeof(teacher));
        while(!infile.eof())
        {
            t1.output();
            infile.read(reinterpret_cast<char*>(&t1),sizeof(teacher));
        }
        cout<<"\n\nRecords Finished ";
    }
    system("pause");
}
void deleterecord()
{
    fstream infile,outfile;
    teacher t1;
    long long check;
    int found=0;
    cout<<"Enter CNIC.No        : ";
    cin>>check;
    infile.open("teacherdata.txt",ios::in);
    if(!infile)
    {
        cout<<"No File Found";
    }
    else
    {
    outfile.open("temp.txt",ios::out);
    infile.seekg(0,ios::beg);
    infile.read(reinterpret_cast<char*>(&t1),sizeof(teacher));
    while(!infile.eof())
    {
        if(t1.getcnicno()!=check)
        {
        outfile.write(reinterpret_cast<char*>(&t1),sizeof(teacher));
        }
    }
    remove("teacherdata.txt");
    rename("temp.txt","teacherdata.txt");
    outfile.close();
    infile.close();
    }
}
void deletefile()
{
    char confirm;
    fstream file;
    file.open("teacherdata.txt");
    cout<<"Your ALL DATA will be LOST.Want to continue?? (y/n) ";
    cin>>confirm;
    if(confirm=='y'|confirm=='Y')
    {
    remove("teacherdata.txt");
    }
}
int main()
{
    int option;
do
{
    system("cls");
    cout<<"\n\n\t1 . Enter New Data ";
    cout<<"\n\n\t2 . Show All Data  ";
    cout<<"\n\n\t3 . Delete Specific Record ";
    cout<<"\n\n\t4 . Delete Complte Record  ";
    cout<<"\n\n\t5 . Quit Program           ";
    cout<<"\n\n\tYour Option  (1-5)         ";
    cin>>option;
switch(option)
{
    case 1:
        writedata();
        break;
    case 2:
        readdata();
        break;
    case 3:
        deleterecord();
        break;
    case 4:
        deletefile();
        break;
    case 5:
        cin.get();
    default:
        cout<<"\n\n\tInvalid Selection ";
}

}while(option!=5);
cout<<"\n\nThank's for Using Program";
    return 0;
}

It looks to me that you need to read the file inside the while loop:

while(infile.read(reinterpret_cast<char*>(&t1),sizeof(teacher)))
{
    if(t1.getcnicno()!=check)
    {
    outfile.write(reinterpret_cast<char*>(&t1),sizeof(teacher));
    }
}

Also checking for eof is not a reliable way of terminating the loop. Using the return value of read is more reliable.

If your question is answered please remember to mark this solved. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.