165 lines
3.6 KiB
C++
165 lines
3.6 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include <conio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
using namespace std;
|
|
|
|
class Student{
|
|
int roll;
|
|
char name[25];
|
|
float marks;
|
|
|
|
|
|
|
|
|
|
|
|
void getdata()
|
|
{
|
|
cout<<"\n\nEnter Roll:";
|
|
cin>>roll;
|
|
|
|
cout<<"\n\nEnter Name:";
|
|
cin>>name;
|
|
|
|
cout<<"\n\nEnter Marks:";
|
|
cin>>marks;
|
|
}
|
|
|
|
void putdata()
|
|
{
|
|
cout<<"\n\t"<<roll<<"\t"<<name<<"\t"<<marks;
|
|
}
|
|
|
|
public:
|
|
void modify();
|
|
int getrno()
|
|
{
|
|
return roll;
|
|
}
|
|
void Display()
|
|
{
|
|
fstream f;
|
|
Student Stu;
|
|
f.open("Student.dat",ios::in|ios::binary);
|
|
cout<<"\n\tRoll\tName\tMarks\n";
|
|
while((f.read((char*)&Stu,sizeof(Stu)))!=NULL)
|
|
Stu.putdata();
|
|
|
|
f.close();
|
|
}
|
|
void AddRecord()
|
|
{
|
|
fstream f;
|
|
Student Stu;
|
|
|
|
f.open("Student.dat",ios::app|ios::binary);
|
|
|
|
Stu.getdata();
|
|
|
|
f.write((char*) &Stu, sizeof(Stu));
|
|
|
|
f.close();
|
|
}
|
|
}stud1,stud;
|
|
|
|
void Student::modify()
|
|
{
|
|
cout<<"Rollno:"<<roll<<"\n";
|
|
cout<<"Name:"<<name<<"\tMarks:"<<marks<<"\n";
|
|
cout<<"Enter new details.\n";
|
|
char nam[20]="";
|
|
float mks;
|
|
cout<<"New name:(Enter '.' to retain old one)";
|
|
cin>>nam;
|
|
cout<<"New marks:(Press -1 to retain old one)";
|
|
cin>>mks;
|
|
if(strcmp(nam,".")!=0){strcpy(name,nam);}
|
|
if(mks!=-1)marks=mks;
|
|
cout<<endl<<marks;
|
|
}
|
|
|
|
void deleter()
|
|
{
|
|
ifstream fd;
|
|
ofstream ft;
|
|
fd.open("Student.dat",ios::binary);
|
|
ft.open("temp.dat",ios::binary);
|
|
int flagd=0;
|
|
//char conf='y';
|
|
Student st;
|
|
cout<<"Enter student rollno to delete:";
|
|
int rno;
|
|
cin>>rno;
|
|
while((fd.read((char*)&st,sizeof(st)))!=NULL)
|
|
{
|
|
if(st.getrno()!=rno)
|
|
{
|
|
flagd=1;
|
|
ft.write((char*)&st,sizeof(st));
|
|
}
|
|
}
|
|
fd.close();
|
|
ft.close();
|
|
if(flagd==0)cout<<"\nSpecified student not found :";
|
|
remove("Student.dat");
|
|
rename("temp.dat","Student.dat");
|
|
remove("temp.dat");
|
|
cout<<"\nProcess complete";
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
int found=0;
|
|
Student S;
|
|
int op;
|
|
fstream fio;
|
|
|
|
do
|
|
{
|
|
cout<<"\n1.Add\n";
|
|
cout<<"2.Display\n";
|
|
cout<<"3.Edit\n";
|
|
cout<<"4.Delete\n";
|
|
cout<<"5.Exit\n";
|
|
cout<<"Enter the options\n";
|
|
cin>>op;
|
|
switch(op){
|
|
case 1:S.AddRecord();break;
|
|
case 2:S.Display();break;
|
|
case 3: fio.open("Student.dat",ios::in|ios::out);
|
|
int rno;
|
|
long pos;
|
|
//char found='f';
|
|
|
|
cout<<"Enter rollno of student whose record is to be modified:";
|
|
cin>>rno;
|
|
cout<<"Beginning"<<fio.tellg();
|
|
system("pause");
|
|
while(!fio.eof())
|
|
{
|
|
pos=fio.tellg();
|
|
cout<<"Position"<<pos<<endl;
|
|
fio.read((char*)&stud1,sizeof(stud1));
|
|
if(stud1.getrno()==rno)
|
|
{
|
|
cout<<"match"<<endl;
|
|
stud1.modify();
|
|
fio.seekg(pos);
|
|
fio.write((char*)&stud1,sizeof(stud1));
|
|
fio.close();
|
|
found=1;
|
|
break;
|
|
}}
|
|
//if (found==0)cout<<"\nRecord not found in the file..!!\n";
|
|
break;
|
|
case 4: deleter();break;
|
|
case 5:exit(0);
|
|
}
|
|
}while(1);
|
|
cout<<"\nData written succesfully...";
|
|
return 0;
|
|
}
|