Saturday, 30 May 2020

file handling: deleting a record in c++

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>
struct emp
{
int emp_id;
int sal;
char name[30];
}e1;
void main()
{       int emp_id;
ifstream fin;
ofstream fout;
fin.open("abc.dat",ios::binary);
if(!fin)
{
cout<<"file not found\n";
getch();
exit(1);
}
fout.open("temp.dat",ios::binary);
if(!fout)
{
cout<<"file not found\n";
getch();
exit(1);
}
cout<<"enter the emp id to delete\n";
cin>>emp_id;
while(!fin.eof())
{
if(!(e1.emp_id==emp_id))
{
fout.write((char*)&e1,sizeof(e1));
}
else
cout<<"yo";

}
fin.close();
remove("abc.dat");
fout.close();
rename("temp.dat","abc.dat");
getch();
}

No comments:

Post a Comment