1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <stdlib.h>
#include <fstream>
#include <string.h>
#include <iostream>
#include<iomanip>
#include "Book.h"
//#include "Student.h"
using namespace std;
fstream lib, lib1, lib2;
//Student student;
Book book1;
void Book::insertBook()
{
lib.open("book.dat",ios::out|ios::app);
book1.newBook();
lib.write((char*)&book1,sizeof(Book));
lib.close();
}
void Book::searchBook(char barcode[6])
{
int sys=0;
lib.open("book.dat",ios::in);
while(lib.read((char*)&book1,sizeof(Book)))
{
if(strcmp(book1.retBarcode(),barcode)==0)
{
book1.displayBook();
sys=1;
}
}
lib.close();
if(sys==0)
cout<<"Error: No such Book found in System." <<endl;
}
void Book::updateBook()
{
int found=0;
char barcode[6];
cout<<"Enter barcode of the Book you want to update:" <<endl;
cin>>barcode;
lib.open("book.dat",ios::in|ios::out);
while(lib.read((char*)&book1,sizeof(Book)) && found==0)
{
if(strcmp(book1.retBarcode(),barcode)==0)
{
book1.displayBook();
cout<<"Enter New Book Details: "<<endl;
book1.editBook();
long pos=-1*sizeof(book1);
lib.seekp(pos,ios::cur);
lib.write((char*)&book1,sizeof(Book));
cout<<"Boobook1dated"<<endl;
found=1;
}
}
lib.close();
if(found==0)
cout<<"Error:Book Not Found. " <<endl;
}
void Book::deleteBook()
{
char barcode[6];
cout<<"Enter barcode of the Book you want to delete: " <<endl;
cin>>barcode;
lib.open("book.dat",ios::in|ios::out);
lib2.open("Temp.dat",ios::out);
lib.seekg(0,ios::beg);
while(lib.read((char*)&book1,sizeof(Book)))
{
if(strcmp(book1.retBarcode(),barcode)!=0)
{
lib2.write((char*)&book1,sizeof(Book));
}
}
lib2.close();
lib.close();
remove("book.dat");
rename("Temp.dat","book.dat");
cout<<"Book deleted." <<endl;
}
void Book::showallBooks()
{
lib.open("book.dat",ios::in);
if(!lib)
{
cout<<"Error: File could not be opened. " <<endl;
return;
}
cout<<"Book Name"<<"Author"<<"Book Number"<<endl;
while(lib.read((char*)&book1,sizeof(Book)))
{
book1.report();
}
lib.close();
}
|