S
shan
#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
class library
{
private:
struct lib
{
char flag;
int id;
string title;
string subject;
}books;
fstream file;
public:
library();
void addrec();
void listrec();
void modifyrec();
void delrec();
void recallrec();
void packrec();
void searchid();
void searchttl();
void exit();
};
//Zero argument constructor
library::library()
{
file.open("library",ios::binary|ios::in|ios:ut);
if(!file)
{
cout<<endl<<"Unable to open file.";
exit();
}
}
//Add record
void library::addrec()
{
char ch;
file.seekp(0L,ios::end);
do
{
cout<<endl<<"Enter book id :";
cin>>books.id;
cout<<endl<<"Enter book title :";
cin>>books.title;
cout<<endl<<"Enter book subject :";
cin>>books.subject;
books.flag=' ';
file.write((char *)&books,sizeof(books));
cout<<"Add another record? (Y/N)";
cin>>ch;
}
while(ch=='y'||ch=='Y');
}
//List all records
void library::listrec()
{
int j=0;
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.flag!='*')
{
cout<<endl<<"Record NO:"<<j++;
cout<<endl<<"ID is "<<books.id;
cout<<endl<<"Subject is "<<books.subject;
cout<<endl<<"Title is "<<books.title;
}
}
file.clear();
cout<<endl<<"Press any key...";
getch();
}
//Modify a record
void library::modifyrec()
{
int tempid;
int count=0;
long int pos;
cout<<"Enter id of book to modify"<<endl;
cin>>tempid;
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.id==tempid)
{
cout<<"Enter new record"<<endl;
cout<<endl<<"Enter book id"<<endl;
cin>>books.id;
cout<<endl<<"Enter book title"<<endl;
cin>>books.title;
cout<<endl<<"Enter book subject"<<endl;
cin>>books.subject;
books.flag=' ';
//place pointer at record which has to
//be over written
pos=count*sizeof(books);
file.seekp(pos,ios::beg);
file.write((char*)&books,sizeof(books));
return;
}
count++;
}
cout<<endl<<"No book with id "<<tempid<<" is in record";
cout<<endl<<"Press any key...";
getch();
file.clear();
}
//Mark a record for deletion
void library::delrec()
{
int tempid;
int count=0;
long int pos;
cout<<"Enter id of book to delete"<<endl;
cin>>tempid;
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.id==tempid)
{
books.flag='*';
pos=count*sizeof(books);
file.seekp(pos,ios::beg);
file.write((char*)&books,sizeof(books));
return;
}
count++;
}
cout<<endl<<"No book with id "<<tempid<<" is in record";
cout<<endl<<"Press any key";
getch();
file.clear();
}
//Recall the record
void library::recallrec()
{
int tempid;
int count=0;
long int pos;
cout<<"Enter id of book "<<endl;
cin>>tempid;
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.id==tempid)
{
books.flag=' ';
pos=count*sizeof(books);
file.seekp(pos,ios::beg);
file.write((char*)&books,sizeof(books));
return;
}
count++;
}
cout<<endl<<"No book with id "<<tempid<<" is in record";
cout<<endl<<"Press any key.....";
getch();
file.clear();
}
//Removes all record permanently
void library:ackrec()
{
//temp file
ofstream outfile;
outfile.open("temp",ios:ut);
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.flag!='*')
outfile.write((char*)&books,sizeof(books));
}
outfile.close();
file.close();
remove("library");
rename("temp","library");
//file.open("library.dat",ios::binary|ios::in|ios:ut|ios::nocreate);
file.open("library",ios::binary|ios::in|ios:ut);
}
//Search by id
void library::searchid()
{
int tempid;
cout<<"Enter id of book to search"<<endl;
cin>>tempid;
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.id==tempid)
{
cout<<endl<<"Record NO:";
cout<<endl<<"ID is "<<books.id;
cout<<endl<<"Subject is "<<books.subject;
cout<<endl<<"Title is "<<books.title;
}
}
cout<<endl<<"Press any key...";
getch();
file.clear();
}
void library::searchttl()
{
string tempttl;
cout<<"Enter title of book to search"<<endl;
cin>>tempttl;
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.title==tempttl)
{
cout<<endl<<"Record NO:";
cout<<endl<<"ID is "<<books.id;
cout<<endl<<"Subject is "<<books.subject;
cout<<endl<<"Title is "<<books.title;
}
}
cout<<endl<<"Press any key...";
getch();
file.clear();
}
void library::exit()
{
file.close();
}
void main()
{
char choice;
library book;
do
{
//clrscr();
cout<<endl<<"1.Add record"<<endl;
cout<<"2.List records"<<endl;
cout<<"3.Modify records"<<endl;
cout<<"4.Delete records"<<endl;
cout<<"5.Recall records"<<endl;
cout<<"6.Pack records"<<endl;
cout<<"7.Search by ID"<<endl;
cout<<"8.Search by title"<<endl;
cout<<"0.EXIT"<<endl;
cout<<"Enter your choice"<<endl;
cin>>choice;
//clrscr();
switch (choice)
{
case '1':
book.addrec();
break;
case'2':
book.listrec();
break;
case'3':
book.modifyrec();
break;
case'4':
book.delrec();
break;
case'5':
book.recallrec();
break;
case'6':
book.packrec();
break;
case'7':
book.searchid();
break;
case'8':
book.searchttl();
break;
case'0':
book.exit();
exit(1);
}
}
while (choice != 0);
}
#include<iostream>
#include<string>
#include<iomanip>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
class library
{
private:
struct lib
{
char flag;
int id;
string title;
string subject;
}books;
fstream file;
public:
library();
void addrec();
void listrec();
void modifyrec();
void delrec();
void recallrec();
void packrec();
void searchid();
void searchttl();
void exit();
};
//Zero argument constructor
library::library()
{
file.open("library",ios::binary|ios::in|ios:ut);
if(!file)
{
cout<<endl<<"Unable to open file.";
exit();
}
}
//Add record
void library::addrec()
{
char ch;
file.seekp(0L,ios::end);
do
{
cout<<endl<<"Enter book id :";
cin>>books.id;
cout<<endl<<"Enter book title :";
cin>>books.title;
cout<<endl<<"Enter book subject :";
cin>>books.subject;
books.flag=' ';
file.write((char *)&books,sizeof(books));
cout<<"Add another record? (Y/N)";
cin>>ch;
}
while(ch=='y'||ch=='Y');
}
//List all records
void library::listrec()
{
int j=0;
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.flag!='*')
{
cout<<endl<<"Record NO:"<<j++;
cout<<endl<<"ID is "<<books.id;
cout<<endl<<"Subject is "<<books.subject;
cout<<endl<<"Title is "<<books.title;
}
}
file.clear();
cout<<endl<<"Press any key...";
getch();
}
//Modify a record
void library::modifyrec()
{
int tempid;
int count=0;
long int pos;
cout<<"Enter id of book to modify"<<endl;
cin>>tempid;
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.id==tempid)
{
cout<<"Enter new record"<<endl;
cout<<endl<<"Enter book id"<<endl;
cin>>books.id;
cout<<endl<<"Enter book title"<<endl;
cin>>books.title;
cout<<endl<<"Enter book subject"<<endl;
cin>>books.subject;
books.flag=' ';
//place pointer at record which has to
//be over written
pos=count*sizeof(books);
file.seekp(pos,ios::beg);
file.write((char*)&books,sizeof(books));
return;
}
count++;
}
cout<<endl<<"No book with id "<<tempid<<" is in record";
cout<<endl<<"Press any key...";
getch();
file.clear();
}
//Mark a record for deletion
void library::delrec()
{
int tempid;
int count=0;
long int pos;
cout<<"Enter id of book to delete"<<endl;
cin>>tempid;
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.id==tempid)
{
books.flag='*';
pos=count*sizeof(books);
file.seekp(pos,ios::beg);
file.write((char*)&books,sizeof(books));
return;
}
count++;
}
cout<<endl<<"No book with id "<<tempid<<" is in record";
cout<<endl<<"Press any key";
getch();
file.clear();
}
//Recall the record
void library::recallrec()
{
int tempid;
int count=0;
long int pos;
cout<<"Enter id of book "<<endl;
cin>>tempid;
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.id==tempid)
{
books.flag=' ';
pos=count*sizeof(books);
file.seekp(pos,ios::beg);
file.write((char*)&books,sizeof(books));
return;
}
count++;
}
cout<<endl<<"No book with id "<<tempid<<" is in record";
cout<<endl<<"Press any key.....";
getch();
file.clear();
}
//Removes all record permanently
void library:ackrec()
{
//temp file
ofstream outfile;
outfile.open("temp",ios:ut);
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.flag!='*')
outfile.write((char*)&books,sizeof(books));
}
outfile.close();
file.close();
remove("library");
rename("temp","library");
//file.open("library.dat",ios::binary|ios::in|ios:ut|ios::nocreate);
file.open("library",ios::binary|ios::in|ios:ut);
}
//Search by id
void library::searchid()
{
int tempid;
cout<<"Enter id of book to search"<<endl;
cin>>tempid;
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.id==tempid)
{
cout<<endl<<"Record NO:";
cout<<endl<<"ID is "<<books.id;
cout<<endl<<"Subject is "<<books.subject;
cout<<endl<<"Title is "<<books.title;
}
}
cout<<endl<<"Press any key...";
getch();
file.clear();
}
void library::searchttl()
{
string tempttl;
cout<<"Enter title of book to search"<<endl;
cin>>tempttl;
file.seekg(0L,ios::beg);
while(file.read((char*)&books,sizeof(books)))
{
if(books.title==tempttl)
{
cout<<endl<<"Record NO:";
cout<<endl<<"ID is "<<books.id;
cout<<endl<<"Subject is "<<books.subject;
cout<<endl<<"Title is "<<books.title;
}
}
cout<<endl<<"Press any key...";
getch();
file.clear();
}
void library::exit()
{
file.close();
}
void main()
{
char choice;
library book;
do
{
//clrscr();
cout<<endl<<"1.Add record"<<endl;
cout<<"2.List records"<<endl;
cout<<"3.Modify records"<<endl;
cout<<"4.Delete records"<<endl;
cout<<"5.Recall records"<<endl;
cout<<"6.Pack records"<<endl;
cout<<"7.Search by ID"<<endl;
cout<<"8.Search by title"<<endl;
cout<<"0.EXIT"<<endl;
cout<<"Enter your choice"<<endl;
cin>>choice;
//clrscr();
switch (choice)
{
case '1':
book.addrec();
break;
case'2':
book.listrec();
break;
case'3':
book.modifyrec();
break;
case'4':
book.delrec();
break;
case'5':
book.recallrec();
break;
case'6':
book.packrec();
break;
case'7':
book.searchid();
break;
case'8':
book.searchttl();
break;
case'0':
book.exit();
exit(1);
}
}
while (choice != 0);
}