PROBLEM : I am trying to write (binary writing) an object of Doubly Linked List onto File & write from it too. I have to write object's complete contents, then load it from file, and store it into new object to re-create the list in FIFO order. I thinking I am writing correctly, but I seriously have no idea how to load (read) it from file. REMEMBER: I am just trying to save and read CONTENTS of a node, & NOT POINTERS.
//template type BOOK class
template<class mytype>
class BOOK
{
private:
staticint count; //declaration of static variable to set ID for books
public:
BOOK<mytype> *next, *prev; //BOOK type pointers; 'next' to store address of
next BOOK & 'prev' to store address of previous BOOK
int ID; //variable to store ID of a book
string bookName;//string to store name of a book
string author; //string to store name of author of book
string book_type;//string to store type of a book
long copies; //variable to store no. of copies a book
long price; //variable to store price of a book
string status; //to store status of a book, either its in stock or not
dynamicQueue<string> book_queue; //created an object of queueClass as data member of each Book
BOOK() //Constructor 0 argument to initialize everything
{
count++; //increment counter
ID=count; //assign counter to ID to be ID of newly added book
next = prev = 0; //Initializing both pointers to 0
bookName = "\0";
author = "\0";
book_type = "\0";
copies = price = 0;
status= "InStock";
}
BOOK(BOOK *n = 0, BOOK *p = 0, string book = "\0", string athr = "\0", string buk_type = "\0", long cp=0, long pr=0) //Constructor multiple arguments, to store information about a book
{
next = n; //store contents of user-given value n into next
prev = p; //store contents of user-given value p into previous
bookName = book;//store contents of user-given value book into bookName
author = athr; //store contents of user-given value athr into author
book_type = buk_type;//store contents of user-given value buk_type into book_type
copies = cp; //store contents of user-given value cp into copies
price = pr; //store contents of user-given value pr into price
status= "InStock";
count++; //increment counter
ID=count; //assign counter to ID to be ID of newly added book
}
};
template <class mytype> // declaration of
int BOOK<mytype>::count=0; // static variable to set ID for books
//--------------------
Main Part for Adding a new Book.
1 2 3 4 5 6 7 8 9 10
BookStoreDataBase<char> obj; //created object of Doubly linked list
string Book, Author, Booktype;
long Copies=1, Price=0;
cout<<"Enter Name of Book = "; cin>>Book;
cout<<"Enter Author = "; cin>>Author;
cout<<"Enter Type of Book = "; cin>>Booktype;
cout<<"Enter Number of Copies = "; cin>>Copies;
cout<<"Enter Price (PKR) = "; cin>>Price;
obj.addBook(Book, Author, Booktype, Copies, Price);
Saving function to save all data to file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
template <class mytype>
void DoublyLinkedList<mytype>::save_data()
{
NODE<mytype> * temp = head; //made copy of head
fstream file; //created new file
file.open("mydata.txt", ios::binary | ios::out | ios::app);
while(temp->next!=0) //Until link list end
{
file.write(reinterpret_cast<char*>(&temp), sizeof(temp));
temp = temp - > next; //move temp to next node
}
file.write(reinterpret_cast<char*>(&temp), sizeof(temp)); //write again for last
//book's data
file.close();
}
Now I have practically no idea on how to read the list from file, store contents into each node & with disturbing the saved arrangement, re-create the list in FIFO order. So I can print it later. I have practiced alot, gone to forums etc etc but not found any concrete Solution. Please help me out. Thanks in advance
This is wrong. First, you're writing the pointers to next and previous, which you don't want to do, second, this isn't going to work with std::string types as members of your class (where sizeof will only get the size of the pointers to dynamic memory in the string object.)
Reading is obviously wrong as well. I would suggest you begin by writing a std::string to file and successfully read it back before you move on to a more complex data structure.