|
| 1 | +// // In the above program add a new feature to search a book in file using book id. if book record found then display it else display book not found. |
| 2 | + |
| 3 | +// // Header files |
| 4 | +#include <iostream> |
| 5 | +#include <fstream> |
| 6 | +#include <cstring> |
| 7 | + |
| 8 | +// // use namespace |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +#define MAX_BOOKS 50 |
| 12 | + |
| 13 | +// // define class Book |
| 14 | +class Book |
| 15 | +{ |
| 16 | +public: |
| 17 | + // // static member variable |
| 18 | + static const unsigned int MAX_CHARS_IN_TITLE = 31; |
| 19 | + |
| 20 | +private: |
| 21 | + // // instance member variables |
| 22 | + unsigned int bookId; |
| 23 | + char bookTitle[MAX_CHARS_IN_TITLE]; |
| 24 | + double bookPrice; |
| 25 | + |
| 26 | +public: |
| 27 | + // // constructors |
| 28 | + Book() |
| 29 | + { |
| 30 | + bookId = -1; |
| 31 | + bookPrice = -1; |
| 32 | + bookTitle[0] = 0; |
| 33 | + } |
| 34 | + |
| 35 | + Book(int bookId, const char *bookTitle, double bookPrice) |
| 36 | + { |
| 37 | + this->bookId = bookId; |
| 38 | + strcpy(this->bookTitle, bookTitle); |
| 39 | + this->bookPrice = bookPrice; |
| 40 | + } |
| 41 | + |
| 42 | + // // instance member function to set bookId |
| 43 | + void setBookId(int bookId) |
| 44 | + { |
| 45 | + if (bookId < 0) // if bookId is negative make it positive |
| 46 | + bookId = -bookId; |
| 47 | + |
| 48 | + this->bookId = bookId; |
| 49 | + } |
| 50 | + |
| 51 | + // // instance member function to get bookId |
| 52 | + unsigned int getBookId() |
| 53 | + { |
| 54 | + return bookId; |
| 55 | + } |
| 56 | + |
| 57 | + // // instance member function to set bookTitle |
| 58 | + void setBookTitle(char *bookTitle) |
| 59 | + { |
| 60 | + strcpy(this->bookTitle, bookTitle); |
| 61 | + } |
| 62 | + |
| 63 | + // // instance member function to get bookTitle |
| 64 | + const char *getBookTitle() |
| 65 | + { |
| 66 | + return bookTitle; |
| 67 | + } |
| 68 | + |
| 69 | + // // instance member function to set bookPrice |
| 70 | + double setBookPrice(double bookPrice) |
| 71 | + { |
| 72 | + this->bookPrice = bookPrice; |
| 73 | + } |
| 74 | + |
| 75 | + // // instance member function to get bookPrice |
| 76 | + double getBookPrice() |
| 77 | + { |
| 78 | + return bookPrice; |
| 79 | + } |
| 80 | + |
| 81 | + // // instance member function to input and set book data |
| 82 | + void inputBookData() |
| 83 | + { |
| 84 | + int bookId; |
| 85 | + char bookTitle[Book::MAX_CHARS_IN_TITLE]; |
| 86 | + double bookPrice; |
| 87 | + |
| 88 | + // // Get book id |
| 89 | + cout << "\nEnter Book Id => "; |
| 90 | + cin >> bookId; |
| 91 | + |
| 92 | + // // Get book title |
| 93 | + cout << "\nEnter Book Title (MAX_CHARS " << Book::MAX_CHARS_IN_TITLE - 1 << ") => "; |
| 94 | + cin.ignore(); |
| 95 | + cin.getline(bookTitle, Book::MAX_CHARS_IN_TITLE); |
| 96 | + |
| 97 | + // // Get book price |
| 98 | + cout << "\nEnter Book Price => "; |
| 99 | + cin >> bookPrice; |
| 100 | + |
| 101 | + // // set data |
| 102 | + setBookId(bookId); |
| 103 | + setBookTitle(bookTitle); |
| 104 | + setBookPrice(bookPrice); |
| 105 | + } |
| 106 | + |
| 107 | + // // instance member function to show book data |
| 108 | + void showBookData() |
| 109 | + { |
| 110 | + cout << "\nBook Id => " << bookId; |
| 111 | + cout << "\nBook Title => " << bookTitle; |
| 112 | + cout << "\nBook Price => " << bookPrice; |
| 113 | + } |
| 114 | + |
| 115 | + // // instance member function to store book record |
| 116 | + int storeBookData() |
| 117 | + { |
| 118 | + if (bookId == -1 || bookPrice == -1) |
| 119 | + return 0; // book data not stored |
| 120 | + |
| 121 | + // // specify file name |
| 122 | + const char *fileName = "books_data.dat"; |
| 123 | + |
| 124 | + // create an instance of ofstream for writing in a file |
| 125 | + ofstream fout; |
| 126 | + |
| 127 | + // // open file in binary mode for writing and append data |
| 128 | + fout.open(fileName, ios::app | ios::binary); |
| 129 | + |
| 130 | + // // check if the file is successfully opened |
| 131 | + if (!fout.is_open()) |
| 132 | + return 0; // book data not stored |
| 133 | + |
| 134 | + fout.write((char *)this, sizeof(*this)); |
| 135 | + |
| 136 | + return 1; // book data successfully stored |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +// // function to fetch books data from a fike and show |
| 141 | +void fetchAndShowBookData() |
| 142 | +{ |
| 143 | + // // specify file name |
| 144 | + const char *fileName = "books_data.dat"; |
| 145 | + |
| 146 | + // create an instance of ifstream for reading from a file |
| 147 | + ifstream fin; |
| 148 | + |
| 149 | + // // open file in binary mode for reading |
| 150 | + fin.open(fileName, ios::in | ios::binary); |
| 151 | + |
| 152 | + // // check if the file is successfully opened |
| 153 | + if (!fin.is_open()) |
| 154 | + { |
| 155 | + cout << "\nError: Unable to Open File..."; |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + // // create an instance of Book to store fetched data |
| 160 | + Book tempBook; |
| 161 | + |
| 162 | + fin.read((char *)&tempBook, sizeof(tempBook)); |
| 163 | + |
| 164 | + while (!fin.eof()) |
| 165 | + { |
| 166 | + cout << endl; |
| 167 | + tempBook.showBookData(); |
| 168 | + cout << endl; |
| 169 | + fin.read((char *)&tempBook, sizeof(tempBook)); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +// // function to search a book from file using book id |
| 174 | +void searchBookById(int bookId) |
| 175 | +{ |
| 176 | + // // specify file name |
| 177 | + const char *fileName = "books_data.dat"; |
| 178 | + |
| 179 | + // create an instance of ifstream for reading from a file |
| 180 | + ifstream fin; |
| 181 | + |
| 182 | + // // open file in binary mode for reading |
| 183 | + fin.open(fileName, ios::in | ios::binary); |
| 184 | + |
| 185 | + // // check if the file is successfully opened |
| 186 | + if (!fin.is_open()) |
| 187 | + { |
| 188 | + cout << "\nError: Unable to Open File..."; |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + // // create an instance of Book to store fetched data |
| 193 | + Book tempBook; |
| 194 | + int found = 0; |
| 195 | + |
| 196 | + fin.read((char *)&tempBook, sizeof(tempBook)); |
| 197 | + |
| 198 | + while (!fin.eof()) |
| 199 | + { |
| 200 | + if (tempBook.getBookId() == bookId) |
| 201 | + { |
| 202 | + cout << "\n>>>>>>>>> Book Found <<<<<<<<" << endl; |
| 203 | + tempBook.showBookData(); |
| 204 | + found = 1; |
| 205 | + break; |
| 206 | + } |
| 207 | + |
| 208 | + fin.read((char *)&tempBook, sizeof(tempBook)); |
| 209 | + } |
| 210 | + |
| 211 | + if (!found) |
| 212 | + cout << "\nThere is No Book Data Stored having Book Id " << bookId << endl; |
| 213 | +} |
| 214 | + |
| 215 | +// // Main Function Start |
| 216 | +int main() |
| 217 | +{ |
| 218 | + int n; |
| 219 | + |
| 220 | + cout << "\nHow Many Books' Data You Want to Store (MAX " << MAX_BOOKS << ") => "; |
| 221 | + cin >> n; |
| 222 | + |
| 223 | + // // invalid input |
| 224 | + if (n < 1 || n > MAX_BOOKS) |
| 225 | + { |
| 226 | + cout << "\n!!! Invalid Input..." << endl; |
| 227 | + return 0; |
| 228 | + } |
| 229 | + |
| 230 | + // // dynamically allocate memory for n objects of Book |
| 231 | + Book *books = new Book[n]; |
| 232 | + |
| 233 | + // // input, set and store books data |
| 234 | + cout << "\n>>>>>>>>>> Enter Data of " << n << " Books <<<<<<<<<<<<<" << endl; |
| 235 | + for (int i = 0; i < n; i++) |
| 236 | + { |
| 237 | + cout << "\n>>>>>>>>>>> Enter Data of Book " << i + 1 << " <<<<<<<<<<<<" << endl; |
| 238 | + |
| 239 | + // // input and set book data |
| 240 | + books[i].inputBookData(); |
| 241 | + |
| 242 | + // // store book data |
| 243 | + if (!(books[i].storeBookData())) |
| 244 | + { |
| 245 | + cout << "\n!!! Book Data Not Stored..." << endl; |
| 246 | + return 0; |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + // // books data stored successfully |
| 251 | + cout << "\nBooks Data Successfully Stored..." << endl; |
| 252 | + |
| 253 | + // // read and show books data |
| 254 | + cout << "\n>>>>>>>>>> Books Data Stored In File <<<<<<<<<<<<<<"; |
| 255 | + fetchAndShowBookData(); |
| 256 | + |
| 257 | + // // get book id to search a book in file |
| 258 | + int bookId; |
| 259 | + |
| 260 | + cout << "\nEnter Book Id to Search A Book => "; |
| 261 | + cin >> bookId; |
| 262 | + |
| 263 | + // // search book |
| 264 | + searchBookById(bookId); |
| 265 | + |
| 266 | + cout << endl; // Add new line |
| 267 | + cin.ignore(); |
| 268 | + return 0; |
| 269 | +} |
| 270 | +// // Main Function End |
0 commit comments