CREATE A STRUCTURE BOOK FOR 20 RECORDS IN BINARY FILE- book_id, book_name, publisher_name, cost

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>

struct book
{
   char book_name[20];
   int book_id;
   char publisher_name[20];
   int cost;
};

void main()
{
   book b1[20];
   ifstream fout;
   ofstream

   fout.open("abc.txt",ios::binary,ios::out);
   if(!fout)
   {
      cout<<"file not exit:";
      exit(1);
   }
   for(int i=0;i<20;i++)
   {
      cout<<"Enter book name:";
      gets(b1[i].book_name);
      cout<<"Enter book id:";
      cin>>b1[i].book_id;
      cout<<"Enter publisher name:";
      gets(b1[i].publisher_name);
      cout<<"Enter cost:";
      cin>>b1[i].cost;

      fout.write((char*)&b1[i],sizeof(b1[i]));
   }
   fout.close();

   fstream fin;
   fin.open("abc.txt",ios::binary,ios::in);
   for(i=0;i<20;i++)
   {
      fin.read((char*)&b1[i],sizeof(b1[i]));

      cout<<"book name is:";
      puts(b1[i].book_name);
      cout<<"book id is:";
      cin>>b1[i].book_id;
      cout<<"publisher name is:";
      puts(b1[i].publisher_name);
      cout<<"cost is:";
      cin>>b1[i].cost;
   }
   fin.close();
   getch();
}

Comments

Popular Posts