Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "heading.h"
- class student
- {
- char gr;
- int marks[5],total,per;
- public:
- char name[30];
- void getData()
- {
- cout<<"Enter Name:";
- cin>>name;
- cout<<"Enter Marks:";
- for(int i=0;i<5;i++)
- {
- cin>>marks[i];
- }
- total=marks[0]+marks[1]+marks[2]+marks[3]+marks[4];
- per=total/5;
- if(per>90)
- gr='A';
- else if(per>70)
- gr='B';
- else if(per>50)
- gr='C';
- else
- gr='D';
- }
- void showData()
- {
- cout<<"Name:"<<name<<endl;
- cout<<"Percentage:"<<per<<"%";
- cout<<"Grade:"<<gr<<endl;
- }
- };
- student obj;
- int pos;
- int p=0;
- void menu()
- {
- cout<<"\t\t\t\tFile Operations"<<endl;
- cout<<"\t\t\t\t~~~~~~~~~~~~~~~"<<endl;
- cout<<"1.Display"<<endl;
- cout<<"2.Append"<<endl;
- cout<<"3.Modify"<<endl;
- cout<<"4.Search"<<endl;
- cout<<"5.Insert"<<endl;
- cout<<"6.Delete"<<endl;
- cout<<"7.Exit"<<endl;
- cout<<"Your Choice:";
- }
- void append()
- {
- obj.getData();
- ofstream ofile;
- ofile.open("grade.txt",ios::app);
- ofile.write((char *)&obj,sizeof(student));
- ofile.close();
- }
- void display()
- {
- ifstream ifile;
- ifile.open("grade.txt");
- while(!ifile.eof())
- {
- ifile.read((char *)&obj,sizeof(student));
- obj.showData();
- cout<<endl;
- }
- ifile.close();
- }
- void search(char nam[])
- {
- ifstream ifile;
- ifile.open("grade.txt");
- while(!ifile.eof())
- {
- ifile.read((char *)&obj,sizeof(student));
- if(strcmp(nam,obj.name)==0)
- {
- p=1;
- obj.showData();
- pos=ifile.tellg();
- }
- }
- if(p==0)
- {
- cout<<"\nError 404:Name not found.";
- cout<<"\nContact Admin at 9790770778";
- }
- ifile.close();
- }
- void modify()
- {
- ofstream ofile("grade.txt",ios::ate);
- int epos;
- char name[30];
- cout<<"Enter name to modify:"<<endl;
- cin>>name;
- cout<<endl;
- search(name);
- if(p!=0)
- {
- epos=pos-sizeof(student);
- obj.getData();
- ofile.seekp(epos);
- ofile.write((char *)&obj,sizeof(student));
- ofile.close();
- }
- }
- void ddelete()
- {
- int xpos,npos=0;
- ofstream ofile("temp.txt");
- ifstream ifile("grade.txt");
- char name[30];
- display();
- cout<<"Input Name you want to delete:";
- cin>>name;
- search(name);
- xpos=pos-sizeof(student);
- while(npos!=xpos)
- {
- ifile.read((char *)&obj,sizeof(student));
- ofile.write((char *)&obj,sizeof(student));
- npos=ifile.tellg();
- }
- xpos=xpos+sizeof(student);
- ifile.seekg(xpos);
- while(!ifile.eof())
- {
- ifile.read((char *)&obj,sizeof(student));
- ofile.write((char *)&obj,sizeof(student));
- }
- ifile.close();
- ofile.close();
- remove("grade.txt");
- rename("temp.txt","grade.txt");
- }
- void insert()
- {
- int npos=0;
- ofstream ofile("temp.txt");
- ifstream ifile("grade.txt");
- char name[30];
- display();
- cout<<"\nAfter what do you want to insert? :";
- cin>>name;
- cout<<endl;
- search(name);
- if(p!=0)
- {
- while(npos!=pos)
- {
- ifile.read((char *)&obj,sizeof(student));
- ofile.write((char *)&obj,sizeof(student));
- npos=ifile.tellg();
- }
- student obj2;
- obj2.getData();
- ofile.write((char *)&obj2,sizeof(student));
- ifile.seekg(npos);
- while(!ifile.eof())
- {
- ifile.read((char *)&obj,sizeof(student));
- ofile.write((char *)&obj,sizeof(student));
- }
- ifile.close();
- ofile.close();
- }
- remove("grade.txt");
- rename("temp.txt","grade.txt");
- }
- void main()
- {
- char name[30];
- int ch;
- while(ch!=7)
- {
- getch();
- clrscr();
- menu();cin>>ch;
- switch(ch)
- {
- case 1:display();break;
- case 2:append();break;
- case 3:modify();break;
- case 4:cout<<"Enter Name to search:";
- cin>>name;
- search(name);break;
- case 5:insert();break;
- case 6:ddelete();break;
- case 7:bye();break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment