Frooster

File Ops

Sep 19th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. #include "heading.h"
  2. class student
  3. {
  4.  char gr;
  5.  int marks[5],total,per;
  6.  public:
  7.  char name[30];
  8.  void getData()
  9.  {
  10.   cout<<"Enter Name:";
  11.   cin>>name;
  12.   cout<<"Enter Marks:";
  13.   for(int i=0;i<5;i++)
  14.   {
  15.    cin>>marks[i];
  16.   }
  17.   total=marks[0]+marks[1]+marks[2]+marks[3]+marks[4];
  18.   per=total/5;
  19.   if(per>90)
  20.    gr='A';
  21.   else if(per>70)
  22.    gr='B';
  23.   else if(per>50)
  24.    gr='C';
  25.   else
  26.    gr='D';
  27.  }
  28.  void showData()
  29.  {
  30.   cout<<"Name:"<<name<<endl;
  31.   cout<<"Percentage:"<<per<<"%";
  32.   cout<<"Grade:"<<gr<<endl;
  33.  }
  34. };
  35.  
  36.  
  37. student obj;
  38. int pos;
  39. int p=0;
  40.  
  41. void menu()
  42. {
  43.  cout<<"\t\t\t\tFile Operations"<<endl;
  44.  cout<<"\t\t\t\t~~~~~~~~~~~~~~~"<<endl;
  45.  cout<<"1.Display"<<endl;
  46.  cout<<"2.Append"<<endl;
  47.  cout<<"3.Modify"<<endl;
  48.  cout<<"4.Search"<<endl;
  49.  cout<<"5.Insert"<<endl;
  50.  cout<<"6.Delete"<<endl;
  51.  cout<<"7.Exit"<<endl;
  52.  cout<<"Your Choice:";
  53. }
  54.  
  55. void append()
  56. {
  57.  obj.getData();
  58.  ofstream ofile;
  59.  ofile.open("grade.txt",ios::app);
  60.  ofile.write((char *)&obj,sizeof(student));
  61.  ofile.close();
  62. }
  63.  
  64. void display()
  65. {
  66.  ifstream ifile;
  67.  ifile.open("grade.txt");
  68.  while(!ifile.eof())
  69.  {
  70.   ifile.read((char *)&obj,sizeof(student));
  71.   obj.showData();
  72.   cout<<endl;
  73.  }
  74.  ifile.close();
  75. }
  76.  
  77. void search(char nam[])
  78. {
  79.  ifstream ifile;
  80.  ifile.open("grade.txt");
  81.  while(!ifile.eof())
  82.  {
  83.   ifile.read((char *)&obj,sizeof(student));
  84.   if(strcmp(nam,obj.name)==0)
  85.   {
  86.    p=1;
  87.    obj.showData();
  88.    pos=ifile.tellg();
  89.   }
  90.  }
  91.  if(p==0)
  92.  {
  93.   cout<<"\nError 404:Name not found.";
  94.   cout<<"\nContact Admin at 9790770778";
  95.  }
  96.  ifile.close();
  97. }
  98.  
  99. void modify()
  100. {
  101.  ofstream ofile("grade.txt",ios::ate);
  102.  int epos;
  103.  char name[30];
  104.  cout<<"Enter name to modify:"<<endl;
  105.  cin>>name;
  106.  cout<<endl;
  107.  search(name);
  108.  if(p!=0)
  109.  {
  110.   epos=pos-sizeof(student);
  111.   obj.getData();
  112.   ofile.seekp(epos);
  113.   ofile.write((char *)&obj,sizeof(student));
  114.   ofile.close();
  115.  }
  116. }
  117.  
  118. void ddelete()
  119. {
  120.  int xpos,npos=0;
  121.  ofstream ofile("temp.txt");
  122.  ifstream ifile("grade.txt");
  123.  char name[30];
  124.  display();
  125.  cout<<"Input Name you want to delete:";
  126.  cin>>name;
  127.  search(name);
  128.  xpos=pos-sizeof(student);
  129.  while(npos!=xpos)
  130.  {
  131.   ifile.read((char *)&obj,sizeof(student));
  132.   ofile.write((char *)&obj,sizeof(student));
  133.   npos=ifile.tellg();
  134.  }
  135.  xpos=xpos+sizeof(student);
  136.  ifile.seekg(xpos);
  137.  while(!ifile.eof())
  138.  {
  139.   ifile.read((char *)&obj,sizeof(student));
  140.   ofile.write((char *)&obj,sizeof(student));
  141.  }
  142.  ifile.close();
  143.  ofile.close();
  144.  remove("grade.txt");
  145.  rename("temp.txt","grade.txt");
  146. }
  147.  
  148. void insert()
  149. {
  150.  int npos=0;
  151.  ofstream ofile("temp.txt");
  152.  ifstream ifile("grade.txt");
  153.  char name[30];
  154.  display();
  155.  cout<<"\nAfter what do you want to insert? :";
  156.  cin>>name;
  157.  cout<<endl;
  158.  search(name);
  159.  if(p!=0)
  160.  {
  161.   while(npos!=pos)
  162.   {
  163.    ifile.read((char *)&obj,sizeof(student));
  164.    ofile.write((char *)&obj,sizeof(student));
  165.    npos=ifile.tellg();
  166.   }
  167.   student obj2;
  168.   obj2.getData();
  169.   ofile.write((char *)&obj2,sizeof(student));
  170.   ifile.seekg(npos);
  171.   while(!ifile.eof())
  172.   {
  173.    ifile.read((char *)&obj,sizeof(student));
  174.    ofile.write((char *)&obj,sizeof(student));
  175.   }
  176.   ifile.close();
  177.   ofile.close();
  178.  }
  179.  remove("grade.txt");
  180.  rename("temp.txt","grade.txt");
  181. }
  182.  
  183. void main()
  184. {
  185.  char name[30];
  186.  int ch;
  187.  while(ch!=7)
  188.  {
  189.   getch();
  190.   clrscr();
  191.   menu();cin>>ch;
  192.   switch(ch)
  193.   {
  194.    case 1:display();break;
  195.    case 2:append();break;
  196.    case 3:modify();break;
  197.    case 4:cout<<"Enter Name to search:";
  198.       cin>>name;
  199.       search(name);break;
  200.    case 5:insert();break;
  201.    case 6:ddelete();break;
  202.    case 7:bye();break;
  203.   }
  204.  }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment