Maintain Book Records
Write a C++ Program to Maintain Book Records using File Handling using File Handling. Here’s simple Program to Maintain Book Records using File Handling in C++ Programming Language.
Example Program in C++ Using file handling to perform following operations:
1) add new record
2) View all records
3) Delete particular record
4) Search record
5) Update record
Below is the source code for C++ Program to Maintain Book Records using File Handling which is successfully compiled and run on Windows System.
SOURCE CODE : :
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
/* C++ Program to Maintain Book Records using File Handling */ #include<iostream> #include<stdio.h> #include<fstream> #include<iomanip> #include<string.h> using namespace std; int menu(); class Book { private: int bookid; char title[20]; float price; protected: int allotbookid(); void showheader(); public: void getbook(); void showbook(); void addbook(); void viewbook(); void searchbook(); void deletebook(); void modifybook(); }; int Book::allotbookid() { ifstream fin; Book temp; int id=0; fin.open("C:\\Users\\acer\\Documents\\books.txt",ios::in|ios::binary); if(!fin) return(id+1); else { fin.read((char*)&temp,sizeof(temp)); while(!fin.eof()) { id=temp.bookid; fin.read((char*)&temp,sizeof(temp)); } id++; return(id); } } void Book::showheader() { cout<<left; cout<<"\n"<<setw(10)<<"BOOK ID"<<setw(10)<<"Price"<<setw(10)<<"Title\n"; } void Book::getbook() { cout<<"Enter Book Title: "; fflush(stdin); gets(title); cout<<"Price of Book: "; cin>>price; bookid=allotbookid(); } void Book::showbook() { cout<<left; cout<<"\n"<<setw(10)<<bookid<<setw(10)<<price<<setw(10)<<title; } void Book::addbook() { ofstream fout; fout.open("C:\\Users\\acer\\Documents\\books.txt",ios::out|ios::app|ios::binary); if(!fout) cout<<"File can not open"; else fout.write((char*)this,sizeof(*this)); fout.close(); } void Book::viewbook() { ifstream fin; fin.open("C:\\Users\\acer\\Documents\\books.txt",ios::in|ios::binary); if(!fin) cout<<"File not found"; else { showheader(); fin.read((char*)this,sizeof(*this)); while(!fin.eof()) { showbook(); fin.read((char*)this,sizeof(*this)); } } fin.close(); } void Book::searchbook() { ifstream fin; char str[20]; fin.open("C:\\Users\\acer\\Documents\\books.txt",ios::in|ios::binary); cout<<"Enter the name of book to search:"; fflush(stdin); gets(str); if(!fin) cout<<"File not found"; else { fin.read((char*)this,sizeof(*this)); while(!fin.eof()) { if(!strcmp(this->title,str)) { showheader(); showbook(); break; } fin.read((char*)this,sizeof(*this)); } if(fin.eof()) cout<<"\nRecord not found"; } fin.close(); } void Book:: modifybook() { int id,r=0; fstream file; file.open("C:\\Users\\acer\\Documents\\books.txt",ios::in|ios::out|ios::ate|ios::binary); cout<<"\nEnter record number to modify (bookid): "; cin>>id; file.seekg(0); if(!file) cout<<"File not found"; else { file.read((char*)this,sizeof(*this)); while(!file.eof()) { r++; if(bookid==id) { showheader(); showbook(); cout<<"\nRe-enter book details:\n"; cout<<"Enter book title:"; fflush(stdin); gets(title); cout<<"Enter book price"; cin>>price; file.seekp((r-1)*sizeof(Book),ios::beg); file.write((char*)this,sizeof(*this)); break; } file.read((char*)this,sizeof(*this)); } if(file.eof()) cout<<"Record not found"; } file.close(); } void Book:: deletebook() { ifstream fin; ofstream fout; int id; char x; fin.open("C:\\Users\\acer\\Documents\\books.txt",ios::in|ios::binary); fout.open("C:\\Users\\acer\\Documents\\tempbooks.txt",ios::out|ios::app|ios::binary); cout<<"Enter bookid to delete record"; cin>>id; if(!fin) cout<<"File not found"; else { fin.read((char*)this,sizeof(*this)); while(!fin.eof()) { if(this->bookid==id) { cout<<"Record you want to delete is:\n\n"; showheader(); showbook(); cout<<"\nAre you sure you want to delete this record(y/n): "; fflush(stdin); cin>>x; if(x=='n') fout.write((char*)this,sizeof(*this)); else cout<<"\nRecord is deleted"; } else fout.write((char*)this,sizeof(*this)); fin.read((char*)this,sizeof(*this)); } fin.close(); fout.close(); system("erase C:\\Users\\acer\\Documents\\books.txt"); system("rename C:\\Users\\acer\\Documents\\tempbooks.txt C:\\Users\\acer\\Documents\\books.txt"); } } int menu() { system("cls"); cout<<"\n1. Add new book"; cout<<"\n2. View all books"; cout<<"\n3. Search book"; cout<<"\n4. modify book"; cout<<"\n5. delete book"; cout<<"\n6. Exit"; cout<<"\n\nEnter your choice"; int ch; cin>>ch; return(ch); } int main() { Book b; int ch; while(1) { ch=menu(); switch(ch) { case 1: b.getbook(); b.addbook(); break; case 2: b.viewbook(); break; case 3: b.searchbook(); break; case 4: b.modifybook(); break; case 5: b.deletebook(); break; case 6: exit(0); default: cout<<"Enter Valid choice"; } } return 0; } |
Above is the source code for C++ Program to Maintain Book Records using File Handling which is successfully compiled and run on Windows System.
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post….
why i cant run it?