#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
class student
{
private:
char name[20],add[20];
char phone[20];
public:
void readdata();
void showdata();
void writerecord();
void readonerecord();
void readallrecord();
void editrecord();
void deleterecord();
};
void student::readdata()
{
cout<<"\n Enter name:";cin>>name;
cout<<"\n enter address:";cin>>add;
cout<<"\n enter your phone number:";cin>>phone;
cout<<endl;
}
void student::showdata()
{
cout<<"name"<<name<<endl<<"address"<<add<<endl<<"phone numner"<<phone<<endl;
cout<<"..................................................................."<<endl;
}
void student::writerecord()
{
ofstream outfile ( "student.txt",ios::binary|ios::app);
outfile.seekp(0,ios::beg);
int z;
cout<<"enter the number of student";cin>>z;
for(int i=0;i<z;i++)
{
readdata();
outfile.write(reinterpret_cast<char*>(this),sizeof(*this));
}
}
void student::readallrecord()
{
ifstream infile("student.txt",ios::binary);
if(!infile)
{
cout<<"file not found:store record first";
return;
}
while(!infile.eof())
{
if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0);
showdata();
}
}
void student::readonerecord()
{
int n;
ifstream infile ("student.txt",ios::binary);
if(!infile)
{
cout<<"file is not found:store data first";
return;
}
infile.seekg(0,ios::end);
int count=infile.tellg()/sizeof(*this);
cout<<"enter record numbar";
cin>>n;
infile.read(reinterpret_cast<char*>(this),sizeof(*this));
showdata();
}
void student::editrecord()
{
int n;
fstream iofile("student.txt",ios::in|ios::binary);
if(!iofile)
{
cout<<"file is not found:reecord first";
return;
}
iofile.seekg(0,ios::end);
int count=iofile.tellg()/sizeof(*this);
cout<<"\n there are "<<cout<<"record in the file";
cout<<"nenter record number to be edit";
cin>>n;
iofile.seekg((n-1)*sizeof(*this));
iofile.read(reinterpret_cast<char*>(this),sizeof(*this));
cout<<"Record" <<n<<"following data"<<endl;
showdata();
iofile.close();
iofile.open("student.txt",ios::out|ios::in|ios::binary);
iofile.seekp((n-1)*sizeof(*this));
cout<<"enter data to be modify"<<endl;
readdata();
iofile.write(reinterpret_cast<char*>(this),sizeof(*this));
}
void student::deleterecord()
{
int n;
ifstream infile ("student.txt",ios::binary);
if(!infile)
{
cout<<"file is not found";
return;
}
infile.seekg(0,ios::end);
int count=infile.tellg()/sizeof(*this);
cout<<"\n there are"<<count<<"record in the file";
cout<<"enter record number to be delete:";
cin>>n;
fstream tmpfile("tempfile",ios::out|ios::binary);
infile.seekg(0);
for(int i=0;i<count;i++)
{
infile.read(reinterpret_cast<char*>(this),sizeof(*this));
if (i==(n-1))
continue;
tmpfile.write(reinterpret_cast<char*>(this),sizeof(*this));
}
infile.close();
ofstream outfile("student.txt",ios::binary);
tmpfile.close();
tmpfile.open(tempfile,ios::in|ios::binary);
for(int i=0;i<count-1;++i)
{
tmpfile.read(reinterpret_cast<char*>(this),sizeof(*this));
showdata();
}
tmpfile.close();
remove(tempfile);
}
int main()
{
student stu;
int choice;
cout<<"**********STUDENT RECORD SYSTEM*********"<<endl;
while(true)
{
cout<<"\n select one option below";
cout<<"\n 1....write record to file";
cout<<"\n 2....show all record from file";
cout<<"\n 3....show one record from file";
cout<<"\n 4.....edit a record";
cout<<"\n 5......delete a record";
cout<<"\n exit from program";
cout<<"\n enter your choice";
cin>>choice;
switch(choice)
{
case 1:
stu.writerecord();
break;
case 2:
stu.readallrecord();
break;
case 3:
stu.readonerecord();
break;
case 4:
stu.editrecord();
break;
case 5:
stu.deleterecord();
break;
case 6:
exit(0);
break;
default:
cout<<"\n choice not availabe";
exit(0);
}
}
system("pause");
return 0;
}
#include<cstdlib>
#include<fstream>
using namespace std;
class student
{
private:
char name[20],add[20];
char phone[20];
public:
void readdata();
void showdata();
void writerecord();
void readonerecord();
void readallrecord();
void editrecord();
void deleterecord();
};
void student::readdata()
{
cout<<"\n Enter name:";cin>>name;
cout<<"\n enter address:";cin>>add;
cout<<"\n enter your phone number:";cin>>phone;
cout<<endl;
}
void student::showdata()
{
cout<<"name"<<name<<endl<<"address"<<add<<endl<<"phone numner"<<phone<<endl;
cout<<"..................................................................."<<endl;
}
void student::writerecord()
{
ofstream outfile ( "student.txt",ios::binary|ios::app);
outfile.seekp(0,ios::beg);
int z;
cout<<"enter the number of student";cin>>z;
for(int i=0;i<z;i++)
{
readdata();
outfile.write(reinterpret_cast<char*>(this),sizeof(*this));
}
}
void student::readallrecord()
{
ifstream infile("student.txt",ios::binary);
if(!infile)
{
cout<<"file not found:store record first";
return;
}
while(!infile.eof())
{
if(infile.read(reinterpret_cast<char*>(this),sizeof(*this))>0);
showdata();
}
}
void student::readonerecord()
{
int n;
ifstream infile ("student.txt",ios::binary);
if(!infile)
{
cout<<"file is not found:store data first";
return;
}
infile.seekg(0,ios::end);
int count=infile.tellg()/sizeof(*this);
cout<<"enter record numbar";
cin>>n;
infile.read(reinterpret_cast<char*>(this),sizeof(*this));
showdata();
}
void student::editrecord()
{
int n;
fstream iofile("student.txt",ios::in|ios::binary);
if(!iofile)
{
cout<<"file is not found:reecord first";
return;
}
iofile.seekg(0,ios::end);
int count=iofile.tellg()/sizeof(*this);
cout<<"\n there are "<<cout<<"record in the file";
cout<<"nenter record number to be edit";
cin>>n;
iofile.seekg((n-1)*sizeof(*this));
iofile.read(reinterpret_cast<char*>(this),sizeof(*this));
cout<<"Record" <<n<<"following data"<<endl;
showdata();
iofile.close();
iofile.open("student.txt",ios::out|ios::in|ios::binary);
iofile.seekp((n-1)*sizeof(*this));
cout<<"enter data to be modify"<<endl;
readdata();
iofile.write(reinterpret_cast<char*>(this),sizeof(*this));
}
void student::deleterecord()
{
int n;
ifstream infile ("student.txt",ios::binary);
if(!infile)
{
cout<<"file is not found";
return;
}
infile.seekg(0,ios::end);
int count=infile.tellg()/sizeof(*this);
cout<<"\n there are"<<count<<"record in the file";
cout<<"enter record number to be delete:";
cin>>n;
fstream tmpfile("tempfile",ios::out|ios::binary);
infile.seekg(0);
for(int i=0;i<count;i++)
{
infile.read(reinterpret_cast<char*>(this),sizeof(*this));
if (i==(n-1))
continue;
tmpfile.write(reinterpret_cast<char*>(this),sizeof(*this));
}
infile.close();
ofstream outfile("student.txt",ios::binary);
tmpfile.close();
tmpfile.open(tempfile,ios::in|ios::binary);
for(int i=0;i<count-1;++i)
{
tmpfile.read(reinterpret_cast<char*>(this),sizeof(*this));
showdata();
}
tmpfile.close();
remove(tempfile);
}
int main()
{
student stu;
int choice;
cout<<"**********STUDENT RECORD SYSTEM*********"<<endl;
while(true)
{
cout<<"\n select one option below";
cout<<"\n 1....write record to file";
cout<<"\n 2....show all record from file";
cout<<"\n 3....show one record from file";
cout<<"\n 4.....edit a record";
cout<<"\n 5......delete a record";
cout<<"\n exit from program";
cout<<"\n enter your choice";
cin>>choice;
switch(choice)
{
case 1:
stu.writerecord();
break;
case 2:
stu.readallrecord();
break;
case 3:
stu.readonerecord();
break;
case 4:
stu.editrecord();
break;
case 5:
stu.deleterecord();
break;
case 6:
exit(0);
break;
default:
cout<<"\n choice not availabe";
exit(0);
}
}
system("pause");
return 0;
}



0 comments:
Speak up your mind
Tell us what you're thinking... !