Program to enter the details of books | C++ Programming
Tuesday, March 03, 2020//program to enter the details of books
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<fstream.h>
#include<stdio.h>
struct BOOK
{ int BookNo;
char BookTitle[20];
int Price;
int Edition;
}obj;
void main()
{ clrscr();
int no;
char p;
ofstream ofile;
ofile.open("BOOKS.DAT",ios::app|ios::binary) ;
if(!ofile)
{ cout<<"\n File can not open";
exit(1);
}
do
{ cout<<"\n Enter the book no:";
cin>>obj.BookNo;
cout<<"\n Enter the book title:";
gets(obj.BookTitle);
cout<<"\n Enter the price of book:";
cin>>obj.Price;
cout<<"\n Enter the Edition:";
cin>>obj.Edition;
ofile.write((char*)&obj,sizeof(obj));
cout<<"\n Do you want to continue:";
cin>>p;
}while(p=='y'||p=='Y');
ofile.close();
ifstream ifile;
ifile.open("BOOKS.DAT",ios::in|ios::binary);
if(!ifile)
{ cout<<"\n File can not read";
exit(1);
}
int flag=0;
cout<<"\n Enter the book number whose details you want to search:";
cin>>no;
while(ifile.read((char *)&obj,sizeof(obj)))
{ if(obj.BookNo==no)
{flag=1;
cout<<"\n the book no:"<<obj.BookNo;
cout<<"\n the book title:"<<obj.BookTitle;
cout<<"\n the price of book:"<<obj.Price;
cout<<"\n the number of copies:"<<obj.Edition;
break;
}
}
if(flag==0)
cout<<"Record not found";
ifile.close();
getch();
}
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<fstream.h>
#include<stdio.h>
struct BOOK
{ int BookNo;
char BookTitle[20];
int Price;
int Edition;
}obj;
void main()
{ clrscr();
int no;
char p;
ofstream ofile;
ofile.open("BOOKS.DAT",ios::app|ios::binary) ;
if(!ofile)
{ cout<<"\n File can not open";
exit(1);
}
do
{ cout<<"\n Enter the book no:";
cin>>obj.BookNo;
cout<<"\n Enter the book title:";
gets(obj.BookTitle);
cout<<"\n Enter the price of book:";
cin>>obj.Price;
cout<<"\n Enter the Edition:";
cin>>obj.Edition;
ofile.write((char*)&obj,sizeof(obj));
cout<<"\n Do you want to continue:";
cin>>p;
}while(p=='y'||p=='Y');
ofile.close();
ifstream ifile;
ifile.open("BOOKS.DAT",ios::in|ios::binary);
if(!ifile)
{ cout<<"\n File can not read";
exit(1);
}
int flag=0;
cout<<"\n Enter the book number whose details you want to search:";
cin>>no;
while(ifile.read((char *)&obj,sizeof(obj)))
{ if(obj.BookNo==no)
{flag=1;
cout<<"\n the book no:"<<obj.BookNo;
cout<<"\n the book title:"<<obj.BookTitle;
cout<<"\n the price of book:"<<obj.Price;
cout<<"\n the number of copies:"<<obj.Edition;
break;
}
}
if(flag==0)
cout<<"Record not found";
ifile.close();
getch();
}
0 Comments