/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html */ /* Binary Files – Book Database Management. Give user option to create, append, read, edit, insert, delete, search, and count total number of records. */ #include <fstream.h> #include <conio.h> #include <stdio.h> #include <string.h> class book { int bookid, noofpages; char name[21]; public: void getdata() { cout<<endl<<“Enter book ID : “; cin>>bookid; cout<<“Enter book name : “; gets(name); cout<<“Enter number of pages in book : “; cin>>noofpages; } void disp() { cout<<endl<<“Displaying data for book” <<endl<<“Book ID : “<<bookid <<endl<<“Book name : “<<name <<“\nNumber of pages : “<<noofpages<<endl; } char* retname() { return name; } int retid() { return bookid; } } obj1, obj2; char ans; fstream file, temp; int ch, flag, r, rec; void create() { file.open(“data”, ios::out | ios::binary); do { obj1.getdata(); file.write((char*)&obj1, sizeof(obj1)); cout<<“Do you wish to enter another record? (y/n): “; cin>>ans; } while (ans == ‘y’ || ans == ‘Y’); file.close(); } void append() { file.open(“data”, ios::app); do { obj1.getdata(); file.write((char*)&obj1, sizeof(obj1)); cout<<“Do you wish to enter another record? (y/n): “; cin>>ans; } while (ans == ‘y’ || ans == ‘Y’); file.close(); } void showfile() { file.open(“data”, ios::in); while (file.read((char*)&obj1, sizeof(obj1))) obj1.disp(); file.close(); } void rewrite() { temp.open(“new”, ios::in); file.open(“data”, ios::out); temp.seekg(0); while (temp.read((char*)&obj2, sizeof(obj2))) file.write((char*)&obj2, sizeof(obj2)); file.close(); temp.close(); remove(“new”); } void editrec() { file.open(“data”, ios::in); temp.open(“new”, ios::out); r = 0; cout<<endl<<“Enter record number to edit : “; cin>>rec; file.seekg(0); while (file.read((char*)&obj2, sizeof(obj2))) { r++; if (r == rec) { cout<<endl<<“Enter new details”; obj1.getdata(); temp.write((char*)&obj1, sizeof(obj1)); } else temp.write((char*)&obj2, sizeof(obj2)); } file.close(); temp.close(); rewrite(); cout<<“\nRecord you wanted to edit has been edited\n”; } void delrec() { file.open(“data”, ios::in); temp.open(“new”, ios::out); r = 1; cout<<endl<<“Enter record number to delete : “; cin>>rec; file.seekg(0); while (file.read((char*)&obj2, sizeof(obj2))) { if (r != rec) temp.write((char*)&obj2, sizeof(obj2)); ++r; } file.close(); temp.close(); rewrite(); cout<<“\nRecord you requested to be removed has been deleted\n”; } void insrec() { file.open(“data”, ios::in); temp.open(“new”, ios::out); r = 0; cout<<endl<<“Enter position at which you want to insert new record : “; cin>>rec; file.seekg(0); while (file.read((char*)&obj2, sizeof(obj2))) { r++; if (r == rec) { cout<<endl<<“Enter details of new record :”; obj1.getdata(); temp.write((char*)&obj1, sizeof(obj1)); temp.write((char*)&obj2, sizeof(obj2)); } else temp.write((char*)&obj2, sizeof(obj2)); } file.close(); temp.close(); rewrite(); cout<<“\nRecord has been inserted\n”; } void insafterid() { file.open(“data”, ios::in); temp.open(“new”, ios::out); cout<<“\nEnter book ID after which you want to insert new record : “; cin>>rec; file.seekg(0); while (file.read((char*)&obj2, sizeof(obj2))) { if (obj2.retid() != rec) temp.write((char*)&obj2, sizeof(obj2)); else { cout<<endl<<“Enter details for new record :”; obj1.getdata(); temp.write((char*)&obj2, sizeof(obj2)); temp.write((char*)&obj1, sizeof(obj1)); } } file.close(); temp.close(); rewrite(); cout<<“\nRecord has been inserted\n”; } void search() { file.open(“data”, ios::in); cout<<endl<<“Search according to” <<endl<<“\t1. ID” <<endl<<“\t2. Name” <<endl<<“Enter your choice : “; cin>>ch; switch(ch) { case 1 : int id; flag = 0; cout<<endl<<“Enter ID to search for : “; cin>>id; while (file.read((char*)&obj1, sizeof(obj1))) { if (obj1.retid() == id) { obj1.disp(); flag = 1; } } file.close(); if (flag == 0) cout<<“\nID you entered does not exist\n”; break; case 2 : char nm[21]; flag = 0; cout<<endl<<“Enter name to search for : “; gets(nm); while (file.read((char*)&obj1, sizeof(obj1))) { if (strcmp(obj1.retname(),nm) == 0) { obj1.disp(); flag = 1; } } file.close(); if (flag == 0) cout<<“\nName you entered does not exist\n”; break; default : cout<<endl<<“You entered an incorrect choice”<<endl; } } void countrec() { r = 0; file.open(“data”, ios::in); while (file.read((char*)&obj1, sizeof(obj1))) ++r; file.close(); cout<<endl<<“Number of records is “<<r<<endl; } void main() { clrscr(); do { cout<<endl<<“Book Data Management Program” <<endl<<“1. Create file” <<endl<<“2. Append file” <<endl<<“3. Read file” <<endl<<“4. Edit nth record” <<endl<<“5. Delete nth record” <<endl<<“6. Insert record at nth position” <<endl<<“7. Insert record after a book ID” <<endl<<“8. Search for a book” <<endl<<“9. Count total number of records” <<endl<<“Enter your choice : “; cin>>ch; switch(ch) { case 1 : create(); break; case 2 : append(); break; case 3 : showfile(); break; case 4 : editrec(); break; case 5 : delrec(); break; case 6 : insrec(); break; case 7 : insafterid(); break; case 8 : search(); break; case 9 : countrec(); break; default : cout<<endl<<“You entered an incorrect choice”; } cout<<“\nDo you want the menu to be shown again? (y/n): “; cin>>ans; } while (ans == ‘y’ || ans == ‘Y’ ); getch(); } /* Output */ Book Data Management Program 1. Create file 2. Append file 3. Read file 4. Edit nth record 5. Delete nth record 6. Insert record at nth position 7. Insert record after a book ID 8. Search for a book 9. Count total number of records Enter your choice : 1 Enter book ID : 1 Enter book name : PhysicsEnter number of pages in book : 145 Do you wish to enter another record? (y / n): y Enter book ID : 2 Enter book name : Chemistry Enter number of pages in book : 214 Do you wish to enter another record? (y / n): n Do you want the menu to be shown again? (y / n) : y...