Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct Book
- {
- char Title[50];
- char ISBN[50];
- char Author[50];
- char publisher[50];
- char Mobile[50];
- };
- istream& operator >> (istream& input, Book& s)
- {
- cout << "Enter Book's Title: ";
- input.getline(s.Title, 50);
- cout << "Enter Book's ISBN: ";
- input.getline(s.ISBN, 50);
- cout << "Enter Book's Author: ";
- input.getline(s.Author, 50);
- cout << "Enter Book's publisher: ";
- input.getline(s.publisher, 50);
- cout << "Enter Book's Mobile: ";
- input.getline(s.Mobile, 50);
- return input;
- }
- void addBook(Book s)
- {
- ofstream file("f.txt", ios::app);
- if (!file)
- {
- cout << "File opening failed!" << endl;
- return;
- }
- short recLength = 0;
- //cin.ignore();
- recLength = strlen(s.publisher) + strlen(s.Title) + strlen(s.Mobile) + strlen(s.ISBN) + strlen(s.Author) + 10;
- file.write((char*)& recLength, sizeof(short));
- short length = strlen(s.Title);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Title, length);
- length = strlen(s.ISBN);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.ISBN, length);
- length = strlen(s.Author);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Author, length);
- length = strlen(s.publisher);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.publisher, length);
- length = strlen(s.Mobile);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Mobile, length);
- file.close();
- cout << "Book added successfully!" << endl;
- }
- /*
- void addIntoAvail()
- {
- fstream file("f.txt", ios::in | ios::out | ios::binary);
- short deleted = 0;
- file.read((char*)&deleted, sizeof(short));
- if (deleted == -1) //avail list is empty
- {
- addBook();
- return;
- }
- file.seekg(deleted, ios::beg);
- short availSize = 0;
- file.read((char*)&availSize, sizeof(short));
- Book s;
- short recLength = 0;
- //cin.ignore();
- cout << "Enter Book's Title: ";
- cin.getline(s.Title, 50);
- recLength += strlen(s.Title);
- cout << "Enter Book's ID: ";
- cin.getline(s.ID, 9);
- recLength += strlen(s.ID);
- cout << "Enter Book's Author: ";
- cin.getline(s.Author, 5);
- recLength += strlen(s.Author);
- cout << "Enter Book's publisher: ";
- cin.getline(s.publisher, 50);
- recLength += strlen(s.publisher);
- cout << "Enter Book's Mobile: ";
- cin.getline(s.Mobile, 12);
- recLength += strlen(s.Mobile);
- recLength += 10;
- if (recLength > availSize)
- {
- file.seekp(0, ios::end);
- file.write((char*)& recLength, sizeof(short));
- short length = strlen(s.Title);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Title, length);
- length = strlen(s.ID);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.ID, length);
- length = strlen(s.Author);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Author, length);
- length = strlen(s.publisher);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.publisher, length);
- length = strlen(s.Mobile);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Mobile, length);
- file.close();
- return;
- }
- file.seekp(deleted+3, ios::beg);
- short next = 0;
- file.read((char*)&next, sizeof(short));
- file.seekp(0, ios::beg);
- file.write((char*)&next, sizeof(short));
- file.seekp(deleted, ios::beg);
- file.write((char*)& availSize, sizeof(short));
- short length = strlen(s.Title);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Title, length);
- length = strlen(s.ID);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.ID, length);
- length = strlen(s.Author);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Author, length);
- length = strlen(s.publisher);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.publisher, length);
- length = strlen(s.Mobile);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Mobile, length);
- short thisRecordOffset = file.tellp();
- thisRecordOffset -= recLength + 2;
- cout << thisRecordOffset << endl;
- cout << "recLength" << recLength << endl;
- if (availSize - recLength < 5)
- {
- while (recLength < availSize)
- {
- file.put('#');
- recLength++;
- }
- file.close();
- return;
- }
- file.seekp(deleted, ios::beg);
- cout << "Avail Size: " << availSize << endl;
- short newSize = availSize - recLength - 2;
- cout << newSize << endl;
- //******************************************
- while (1)
- {
- short currOffset = file.tellg();
- file.read((char*)&availSize, sizeof(short));
- file.seekg(1, ios::cur);
- short nextOffset = 0;
- file.read((char*)&nextOffset, sizeof(short));
- if (nextOffset == -1) //wselt lel a5er
- {
- file.seekp(-2, ios::cur);
- file.write((char*)&thisRecordOffset, sizeof(short));
- file.seekg(thisRecordOffset, ios::beg);
- file.write((char*)&newSize, sizeof(short));
- file.put('*');
- short temp = -1;
- file.write((char*)&temp, sizeof(short));
- short x = 3;
- while (x < newSize)
- {
- file.put('#');
- x++;
- }
- file.write((char*)&recLength, sizeof(short));
- length = strlen(s.Title);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Title, length);
- length = strlen(s.ID);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.ID, length);
- length = strlen(s.Author);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Author, length);
- length = strlen(s.publisher);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.publisher, length);
- length = strlen(s.Mobile);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Mobile, length);
- file.close();
- return;
- }
- }
- }
- */
- void addIntoAvail(Book s)
- {
- fstream file("f.txt", ios::in | ios::out | ios::binary);
- short deleted = 0;
- file.read((char*)&deleted, sizeof(short));
- if (deleted == -1) //avail list is empty
- {
- addBook(s);
- file.close();
- return;
- }
- file.seekg(deleted, ios::beg);
- short availSize = 0;
- file.read((char*)&availSize, sizeof(short));
- short recLength = strlen(s.publisher) + strlen(s.Title) + strlen(s.Mobile) + strlen(s.ISBN) + strlen(s.Author) + 10;
- if (recLength > availSize)
- {
- addBook(s);
- file.close();
- return;
- }
- file.seekp(deleted + 3, ios::beg);
- short next = 0;
- file.read((char*)&next, sizeof(short));
- file.seekp(0, ios::beg);
- file.write((char*)&next, sizeof(short));
- file.seekp(deleted, ios::beg);
- file.write((char*)& availSize, sizeof(short));
- short length = strlen(s.Title);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Title, length);
- length = strlen(s.ISBN);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.ISBN, length);
- length = strlen(s.Author);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Author, length);
- length = strlen(s.publisher);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.publisher, length);
- length = strlen(s.Mobile);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Mobile, length);
- short thisRecordOffset = file.tellp();
- thisRecordOffset -= recLength + 2;
- if (availSize - recLength < 5)
- {
- while (recLength < availSize)
- {
- file.put('#');
- recLength++;
- }
- file.close();
- cout << "Book added successfully!" << endl;
- return;
- }
- file.seekp(deleted, ios::beg);
- short newSize = availSize - recLength - 2;
- //******************************************
- while (1)
- {
- short currOffset = file.tellg();
- file.read((char*)&availSize, sizeof(short));
- file.seekg(1, ios::cur);
- short nextOffset = 0;
- file.read((char*)&nextOffset, sizeof(short));
- if (nextOffset == -1) //wselt lel a5er
- {
- file.seekp(-2, ios::cur);
- file.write((char*)&thisRecordOffset, sizeof(short));
- file.seekg(thisRecordOffset, ios::beg);
- file.write((char*)&newSize, sizeof(short));
- file.put('*');
- short temp = -1;
- file.write((char*)&temp, sizeof(short));
- short x = 3;
- while (x < newSize)
- {
- file.put('#');
- x++;
- }
- file.write((char*)&recLength, sizeof(short));
- length = strlen(s.Title);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Title, length);
- length = strlen(s.ISBN);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.ISBN, length);
- length = strlen(s.Author);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Author, length);
- length = strlen(s.publisher);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.publisher, length);
- length = strlen(s.Mobile);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Mobile, length);
- file.close();
- cout << "Book added successfully!" << endl;
- return;
- }
- }
- }
- int searchFile(char ISBN[])
- {
- short tempLength = 0;
- ifstream file("f.txt");
- int indexOfRecord = 0;
- file.read((char*)&tempLength, sizeof(short)); //header
- while (1) {
- indexOfRecord = file.tellg();
- file.read((char*)&tempLength, sizeof(short));
- if (file.fail())
- break;
- char c;
- file.get(c);
- if (c == '*')
- {
- file.seekg(-1, ios::cur);
- file.seekg(tempLength, ios::cur);
- continue;
- }
- else
- file.seekg(-1, ios::cur);
- file.read((char*)&tempLength, sizeof(short));
- file.seekg(tempLength, ios::cur);
- file.read((char*)&tempLength, sizeof(short));
- char tempBuf[9];
- file.read((char*)&tempBuf, tempLength);
- tempBuf[tempLength] = 0;
- bool flag = false;
- for (int i = 0; i < strlen(tempBuf); i++)
- {
- if (tempBuf[i] != ISBN[i])
- {
- flag = true;
- break;
- }
- }
- if (!flag)
- {
- file.close();
- return indexOfRecord;
- }
- file.read((char*)&tempLength, sizeof(short));
- file.seekg(tempLength, ios::cur);
- file.read((char*)&tempLength, sizeof(short));
- file.seekg(tempLength, ios::cur);
- file.read((char*)&tempLength, sizeof(short));
- file.seekg(tempLength, ios::cur);
- }
- file.close();
- return -1;
- }
- void deleteBook(char temp[])
- {
- fstream file("f.txt", ios::in | ios::out | ios::binary);
- int offset = searchFile(temp);
- if (offset == -1)
- {
- cout << "Record not found" << endl;
- file.close();
- return;
- }
- file.seekg(offset, ios::beg);
- short recSize = 0;
- file.read((char*)&recSize, sizeof(short));
- file.seekg(0, ios::beg);
- short deleted = 0;
- file.read((char*)&deleted, sizeof(short));
- if (deleted == -1)
- {
- file.seekp(0, ios::beg);
- file.write((char*)&offset, sizeof(short));
- file.seekp(offset + sizeof(short), ios::beg);
- file.put('*');
- offset = -1;
- file.write((char*)&offset, sizeof(short));
- file.close();
- cout << "Book deleted successfully!" << endl;
- return;
- }
- int prev = 0;//first it's the header
- while (1)
- {
- file.seekg(deleted, ios::beg);
- short size = 0;
- file.read((char*)&size, sizeof(short));
- if (recSize >= size)
- {
- file.seekp(deleted, ios::beg);
- file.seekp(prev, ios::beg);
- file.write((char*)&offset, sizeof(short));
- file.seekp(offset + sizeof(short), ios::beg);
- file.put('*');
- file.write((char*)&deleted, sizeof(short));
- file.close();
- cout << "Book deleted successfully!" << endl;
- return;
- }
- file.seekg(1, ios::cur);
- prev = file.tellg();
- file.read((char*)&deleted, sizeof(short));
- if (deleted == -1)
- {
- file.seekp(-2, ios::cur);
- file.write((char*)&offset, sizeof(short));
- file.seekp(offset + sizeof(short), ios::beg);
- file.put('*');
- file.write((char*)&deleted, sizeof(short));
- file.close();
- cout << "Book deleted successfully!" << endl;
- return;
- }
- }
- }
- void printAvailList()
- {
- ifstream file("f.txt");
- short offset = 0;
- file.read((char*)&offset, sizeof(short));
- cout << "Avail List: \n";
- while (offset != -1)
- {
- cout << offset << endl;
- file.seekg(offset + sizeof(short)+1, ios::beg);
- file.read((char*)&offset, sizeof(short));
- }
- }
- void displayBook()
- {
- cout << "Please enter Book's ISBN: ";
- char ISBN[50];
- cin.getline(ISBN, 50);
- ISBN[10] = 0;
- int offset = searchFile(ISBN);
- if (offset == -1)
- {
- cout << "Book not found!\n";
- return;
- }
- ifstream file("f.txt");
- file.seekg(offset, ios::beg);
- short length = 0;
- file.read((char*)&length, sizeof(short));
- char buf[51];
- file.read((char*)&length, sizeof(short));
- file.read((char*)&buf, length);
- buf[length] = 0;
- cout << "Title: " << buf << endl;
- file.read((char*)&length, sizeof(short));
- file.read((char*)&buf, length);
- buf[length] = 0;
- cout << "ISBN: " << buf << endl;
- file.read((char*)&length, sizeof(short));
- file.read((char*)&buf, length);
- buf[length] = 0;
- cout << "Author: " << buf << endl;
- file.read((char*)&length, sizeof(short));
- file.read((char*)&buf, length);
- buf[length] = 0;
- cout << "publisher: " << buf << endl;
- file.read((char*)&length, sizeof(short));
- file.read((char*)&buf, length);
- buf[length] = 0;
- cout << "Mobile: " << buf << endl;
- file.close();
- }
- void updateBook()
- {
- cout << "Enter Book's ISBN to be updated: " << endl;
- char ISBN[50];
- cin.getline(ISBN, 50);
- short offset = searchFile(ISBN);
- if (offset == -1)
- {
- cout << "Book not found!" << endl;
- return;
- }
- fstream file("f.txt", ios::out | ios::in);
- file.seekg(offset, ios::beg);
- short oldSize = 0;
- file.read((char*)&oldSize, sizeof(short));
- cout << "Enter new data: " << endl;
- Book s;
- cin >> s;
- short recLength = strlen(s.publisher) + strlen(s.Title) + strlen(s.Mobile) + strlen(s.ISBN) + strlen(s.Author) + 10;
- if (recLength == oldSize)
- {
- file.seekp(offset, ios::beg);
- file.write((char*)& recLength, sizeof(short));
- short length = strlen(s.Title);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Title, length);
- length = strlen(s.ISBN);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.ISBN, length);
- length = strlen(s.Author);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Author, length);
- length = strlen(s.publisher);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.publisher, length);
- length = strlen(s.Mobile);
- file.write((char*)& length, sizeof(short));
- file.write((char*)& s.Mobile, length);
- file.close();
- cout << "Book updated successfully!" << endl;
- return;
- }
- else
- {
- deleteBook(ISBN);
- addIntoAvail(s);
- cout << "Book updated successfully!" << endl;
- }
- }
- void insertDeleted(short nodeSize, short startOffset)
- {
- short range = startOffset + nodeSize;
- fstream file("f.txt", ios::in | ios::out);
- short deletedOffset;
- file.read((char*)&deletedOffset, sizeof(short));
- if (deletedOffset == startOffset)
- {
- return;
- }
- while (deletedOffset != -1)
- {
- if (deletedOffset > startOffset && deletedOffset < range)
- {
- file.seekg(3, ios::cur);
- file.read((char*)&deletedOffset, sizeof(short));
- continue;
- }
- }
- }
- void compactAvailList()
- {
- short header = 0;
- fstream file("f.txt", ios::in | ios::out);
- file.read((char*)&header, sizeof(short));
- if (header == -1)
- {
- cout << "Nothing to be comapcted!" << endl;
- return;
- }
- file.seekg(2, ios::beg);
- short cumulativeSize = 0;
- short numOfAdjacentRec = 0;
- short startOffset = 0;
- while (!file.fail())
- {
- short curSize;
- file.read((char*)&curSize, sizeof(short));
- if (file.fail())
- break;
- char c;
- file.get(c);
- if (c == '*' && numOfAdjacentRec == 0)
- {
- file.seekg(-1, ios::cur);
- startOffset = file.tellg();
- startOffset -= 2;
- cumulativeSize += curSize;
- numOfAdjacentRec++;
- file.seekg(curSize, ios::cur);
- }
- else if (c == '*')
- {
- cumulativeSize += curSize;
- numOfAdjacentRec++;
- file.seekg(-1, ios::cur);
- file.seekg(curSize, ios::cur);
- }
- else
- {
- if (numOfAdjacentRec != 0)
- {
- cumulativeSize += (numOfAdjacentRec - 1) * 2; //2 bytes for each short
- file.seekg(startOffset);
- file.write((char*)&cumulativeSize, sizeof(short));
- cumulativeSize = 0;
- numOfAdjacentRec = 0;
- continue;
- }
- file.seekg(-1, ios::cur);
- file.seekg(curSize, ios::cur);
- }
- }
- if (numOfAdjacentRec != 0)
- {
- fstream f("f.txt", ios::in | ios::out);
- cumulativeSize += (numOfAdjacentRec - 1) * 2; //2 bytes for each short
- f.seekp(startOffset, ios::beg);
- f.write((char*)&cumulativeSize, sizeof(short));
- f.close();
- return;
- }
- }
- void printAllData()
- {
- fstream file("f.txt", ios::in);
- file.seekg(2, ios::beg);
- while (1)
- {
- short length = 0;
- file.read((char*)&length, sizeof(short));
- if (file.fail())
- return;
- char c;
- file.get(c);
- if (c == '*')
- {
- file.seekg(-1, ios::cur);
- file.seekg(length, ios::cur);
- continue;
- }
- file.seekg(-1, ios::cur);
- char buf[51];
- file.read((char*)&length, sizeof(short));
- file.read((char*)&buf, length);
- buf[length] = 0;
- cout << "Title: " << buf << endl;
- file.read((char*)&length, sizeof(short));
- file.read((char*)&buf, length);
- buf[length] = 0;
- cout << "ISBN: " << buf << endl;
- file.read((char*)&length, sizeof(short));
- file.read((char*)&buf, length);
- buf[length] = 0;
- cout << "Author: " << buf << endl;
- file.read((char*)&length, sizeof(short));
- file.read((char*)&buf, length);
- buf[length] = 0;
- cout << "publisher: " << buf << endl;
- file.read((char*)&length, sizeof(short));
- file.read((char*)&buf, length);
- buf[length] = 0;
- cout << "Mobile: " << buf << endl;
- file.close();
- }
- }
- int main()
- {
- fstream file("f.txt", ios::trunc | ios::out | ios::binary);
- short header = -1;
- file.write((char*)&header, sizeof(short));
- file.close();
- cout << "*********************************************************\n";
- cout << "\t\tBook's Management System\n";
- cout << "*********************************************************\n\n";
- cout << "1) Add new Book" << endl;
- cout << "2) Update Book" << endl;
- cout << "3) Delete Book" << endl;
- cout << "4) Display Book" << endl;
- cout << "5) Compact available records list" << endl;
- cout << "6) Exit\n" << endl;
- cout << "Please enter Your choice: ";
- int choice;
- while (cin >> choice)
- {
- cin.ignore();
- if (choice == 1){
- Book s;
- cin >> s;
- addIntoAvail(s);
- }
- else if (choice == 2)
- {
- updateBook();
- }
- else if (choice == 3)
- {
- cout << "Enter Book's ISBN to be deleted: " << endl;
- char ISBN[10];
- cin.getline(ISBN, 50);
- deleteBook(ISBN);
- }
- else if (choice == 4)
- displayBook();
- else if (choice == 5)
- {
- compactAvailList();
- }
- else
- break;
- cout << "1) Add new Book" << endl;
- cout << "2) Update Book" << endl;
- cout << "3) Delete Book" << endl;
- cout << "4) Display Book" << endl;
- cout << "5) Compact available records list" << endl;
- cout << "6) Exit" << endl;
- cout << "Please enter Your choice: ";
- }
- //printAllData();
- file.open("f.txt", ios::in);
- file.seekg(59, ios::beg);
- file.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement