sahajjain01

Binary File Handling

Feb 12th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include<fstream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. class BOOK
  6. {
  7.     int b_no;
  8.     char b_name[50];
  9.     public:
  10.     void getdetails()
  11.     {
  12.         cout<<"\n Enter book no.: ";
  13.         cin>>b_no;
  14.         cout<<"\n Enter book name: ";
  15.         gets(b_name);
  16.     }
  17.     void showdetails()
  18.     {
  19.         cout<<"\n Book no.: "<<b_no;
  20.         cout<<"\n Book name: ";
  21.         puts(b_name);
  22.     }
  23.     int return_b_no() {return b_no;}
  24. };
  25. void insert()
  26. {
  27.     BOOK obj;
  28.     obj.getdetails();
  29.     ofstream ofile;
  30.     ofile.open("BOOK.DAT",ios::app|ios::binary);
  31.     ofile.write((char*)&obj,sizeof(obj));
  32.     ofile.close();
  33.     cout<<"\n Insert successful. Press any key to continue...";
  34.     getch();
  35. }
  36. void display()
  37. {
  38.     BOOK obj;
  39.     ifstream ifile;
  40.     ifile.open("BOOK.DAT");
  41.     while(ifile.read((char*)&obj,sizeof(obj)))
  42.     {obj.showdetails();}
  43.     ifile.close();
  44.     cout<<"\n Press any key to continue...";
  45.     getch();
  46. }
  47. void search()
  48. {
  49.     BOOK obj;
  50.     int b_no,token=0;
  51.     cout<<"\n Enter book no. to be searched: ";
  52.     cin>>b_no;
  53.     ifstream ifile;
  54.     ifile.open("BOOK.DAT");
  55.     while(ifile.read((char*)&obj,sizeof(obj)))
  56.     {
  57.         if(obj.return_b_no()==b_no)
  58.         {
  59.             cout<<"\n Record found!";
  60.             token=1;
  61.             obj.showdetails();
  62.         }
  63.     }
  64.     ifile.close();
  65.     if(token==0)
  66.     cout<<"\n Record not found.";
  67.     cout<<"\n Press any key to continue...";
  68.     getch();
  69. }
  70. void count()
  71. {
  72.     BOOK obj;
  73.     int count=0;
  74.     ifstream ifile;
  75.     ifile.open("BOOK.DAT");
  76.     while(ifile.read((char*)&obj,sizeof(obj)))
  77.     count++;
  78.     ifile.close();
  79.     cout<<"\n Number of records in the file: "<<count;
  80.     cout<<"\n Press any key to continue...";
  81.     getch();
  82. }
  83. void modify()
  84. {
  85.     BOOK obj;
  86.     int b_no,token=0;
  87.     int pos ;
  88.     cout<<"\n Enter book no. to be modified: ";
  89.     cin>>b_no;
  90.     fstream file;
  91.     file.open("BOOK.DAT",ios::binary|ios::in|ios::out);
  92.     while(file.read((char*)&obj,sizeof(obj)))
  93.     {
  94.         pos=-1*sizeof(obj) ;
  95.         if(obj.return_b_no()==b_no)
  96.         {
  97.             token=1;
  98.             obj.showdetails();
  99.             cout<<"\n Modify:\n";
  100.             file.seekg(pos,ios::cur);
  101.             obj.getdetails();
  102.             file.write((char*)&obj,sizeof(obj));
  103.             cout<<"\n Modify successful!";
  104.         }
  105.     }
  106.     if(token==0)
  107.     cout<<"\n Record not found.";
  108.     cout<<"\n Press any key to continue...";
  109.     file.close();
  110.     getch();
  111. }
  112. void main()
  113. {
  114.     while(1)
  115.     {
  116.         clrscr();
  117.         cout<<"\n\t\tMain Menu:-";
  118.         cout<<"\n\n\t1.Insert";
  119.         cout<<"\n\t2.Dispay";
  120.         cout<<"\n\t3.Search";
  121.         cout<<"\n\t4.Count";
  122.         cout<<"\n\t5.Modify";
  123.         cout<<"\n\t6.Exit";
  124.         cout<<"\n\n\tEnter choice: ";
  125.         int choice;
  126.         cin>>choice;
  127.         switch(choice)
  128.         {
  129.             case 1:
  130.             insert();
  131.             break;
  132.  
  133.             case 2:
  134.             display();
  135.             break;
  136.  
  137.             case 3:
  138.             search();
  139.             break;
  140.  
  141.             case 4:
  142.             count();
  143.             break;
  144.  
  145.             case 5:
  146.             modify();
  147.             break;
  148.  
  149.             case 6:
  150.             exit(0);
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment