You are on page 1of 3

#include<stdio.

h>
#include<conio.h>
#include<fstream.h>
#include<process.h>
class student
{
private:
char name[10];
char branch[10];
int id;
char semester[10];
public:
void getdata();
void saveinfile();
void readfromfile();
void display();
void search();
void deleted();
};
void student:: getdata()
{
cout<<"enter the name";
cin>>name;
cout<<"enter the branch";
cin>>branch;
cout<<"enter the semester";
cin>>semester;
cout<<"enter id";
cin>>id;
this->saveinfile();

}
void student::saveinfile()
{
fstream obj;
obj.open("record.txt",ios::app);
obj.write((char*)this,sizeof(student));
obj.close();
}
void student::readfromfile()
{
fstream obj;
obj.open("record.txt",ios::in|ios::nocreate);
if(!obj.fail())
{
obj.read((char*)this,sizeof(student));
while(!obj.eof())
{

this->display();
obj.read((char*)this,sizeof(student));
}
}
obj.close();
}
void student::display()
{
cout<<name<<endl;
cout<<branch<<endl;
cout<<semester<<endl;
cout<<id<<endl;
}
void student::deleted()
{
int id2;
fstream obj,obj1;
cout<<"enter id to be deleted";
cin>>id2;

obj.open("record.txt",ios::in|ios::nocreate);
obj1.open("new.txt",ios::app);
obj.read((char*)this,sizeof(student));
while(!obj.eof())
{

if(this->id!=id2)
{
obj1.write((char*)this,sizeof(student));
}
obj.read((char*)this,sizeof(student));
}
remove("record.txt");
rename("new.txt","record.txt");

}
void student::search()
{
int id1;
fstream obj;
obj.open("record.txt",ios::in);
obj.read((char*)this,sizeof(student));
cout<<"enter the id to search";
cin>>id1;
while(!obj.eof())
{
if(this->id==id1)
{
cout<<endl<<"record is found"<<endl;
int l=obj.tellg();
obj.seekg(l- sizeof(student));
obj.read((char *)this,sizeof(student));
this->display();
break;
}
obj.read((char *)this,sizeof(student));
}
obj.close();
}
void main()
{
student s1;
clrscr();
int x;
while(1)
{
cout<<" MENU"<<endl;
cout<<"1..add,2..display,3..search,4..remove,5..exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>x;
switch(x)
{
case 1:
s1.getdata();
break;
case 2:
s1.readfromfile();
break;
case 3:
s1.search();
break;
case 4:
s1.deleted();
break;
case 5:
exit(0);
}
}
}

You might also like