Friday, August 5, 2011

kie here's anoder prog..11) To store Book Number, Name, Price and Author. Display the file and delete a record by reading the Book Number.


#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class book
{  private: float price;
   char bname[20];
   char author[20];
   int bno;

public:
void getdata()
{cout<<"\nEnter book no      : ";   cin>>bno;
 cout<<"\nEnter book name    : ";    gets(bname);
 cout<<"\nEnter Author name  : ";    gets(author);
 cout<<"\nEnter Price        : ";    cin>>price;
}

void putdata()
{cout<<"\nBook no      : "<<bno;
 cout<<"\nBook    Name : "<<bname;
 cout<<"\nAuthor       : "<<author;
 cout<<"\nPrice        : "<<price;
}
int retbno(){return(bno);}
}bk;

void create()
{ int n; cout<<"\nEnter no: of records"; cin>>n;
  ofstream fout;
  fout.open("Book.dat",ios::binary|ios::out);
   for(int i=0;i<n;i++)
   {bk.getdata();
    fout.write((char*)&bk,sizeof(bk));
    }
}


void delete_()
{ cout<<"\nEnter book no: of record to be deleted : ";
  int no; cin>>no;
  ifstream fin;
  fin.open("Book.dat",ios::binary|ios::in);
  ofstream fout;
  fout.open("temp.dat",ios::binary|ios::out);
  while(fin)
  {fin.read((char*)&bk,sizeof(bk));
   if(bk.retbno()!=no)
    {fout.write((char*)&bk,sizeof(bk));}
  }
    fin.close();
    fout.close();
    remove("Book.dat");
    rename("Temp.dat","Book.dat");
}

void read_and_disp()
{
 ifstream fin;
 fin.open("Book.dat");

  while(fin.read((char*)&bk,sizeof(bk)))
   {bk.putdata();break;}

}

void main()
{clrscr();
 create();
 delete_();
 read_and_disp();
 getch();
 }

No comments:

Post a Comment