title: File Handling with LinkList in C++
body: <p><strong>PROBLEM:</strong> I am trying to write (binary writing) an object of Doubly Linked List onto File &amp; 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.</p>
  
  <p>REMEMBER: I am just trying to save and read <code>CONTENTS</code> of a node, &amp; <code>NOT POINTERS.</code></p>
  
  <p>CODE:</p>
  
  <pre><code>//template type BOOK class
  
  template&lt;class mytype&gt;
  class BOOK      
  {
  private:
      static int count;   //declaration of static variable to set ID for books
  public:
      BOOK&lt;mytype&gt; *next, *prev;  //BOOK type pointers; 'next' to store address of 
  next BOOK &amp; '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&lt;string&gt; 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 &lt;class mytype&gt;    // declaration of
  int BOOK&lt;mytype&gt;::count=0; // static variable to set ID for books
  //--------------------
  </code></pre>
  
  <p>Main Part for Adding a new Book.</p>
  
  <pre><code>BookStoreDataBase&lt;char&gt; obj;    //created object of Doubly linked list
  string Book, Author, Booktype;
  long Copies=1, Price=0;
  cout&lt;&lt;"Enter Name of Book = ";  cin&gt;&gt;Book;
  cout&lt;&lt;"Enter Author = ";        cin&gt;&gt;Author;
  cout&lt;&lt;"Enter Type of Book = ";  cin&gt;&gt;Booktype;
  cout&lt;&lt;"Enter Number of Copies = ";  cin&gt;&gt;Copies;
  cout&lt;&lt;"Enter Price (PKR) = ";   cin&gt;&gt;Price;
  
  obj.addBook(Book, Author, Booktype, Copies, Price);
  </code></pre>
  
  <p>Saving function to save all data to file</p>
  
  <pre><code>template &lt;class mytype&gt;
  void DoublyLinkedList&lt;mytype&gt;::save_data()
  {
      NODE&lt;mytype&gt; * temp = head; //made copy of head
      fstream file;     //created new file
      file.open("mydata.txt", ios::binary | ios::out | ios::app);
  
      while(temp-&gt;next!=0) //Until link list end
      {
            file.write(reinterpret_cast&lt;char*&gt;(&amp;temp), sizeof(temp));
            temp = temp - &gt; next; //move temp to next node
      }
      file.write(reinterpret_cast&lt;char*&gt;(&amp;temp), sizeof(temp)); //write again for last  
                                                                //book's data
      file.close();
  }
  </code></pre>
  
  <p><em>Now I have practically no idea on how to read the list from file, store contents into each node &amp; 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</em></p>
  
  <hr>
  
  <p>A sample of my effort</p>
  
  <pre><code>template &lt;class mytype&gt;
  void DoublyLinkedList&lt;mytype&gt;::load_data()
  {
      fstream file;
      file.open("mydata.txt", ios::binary | ios::in);
      while(!file.eof())
      {
          NODE&lt;mytype&gt; *temp = new NODE&lt;mytype&gt;;
          file.read(reinterpret_cast&lt;char*&gt;(&amp;temp), sizeof(temp));
          if(is_Empty())
          {
              head = tail = temp;
          }
          else
          {
              temp-&gt;prev-&gt;next = temp;
              temp-&gt;next=0;
          }
      }
      file.close();
  }
  //-------------------
  </code></pre>
  
  <p>NO COMPILE TIME ERROR.</p>
  
  <p><strong>RUNTIME ERROR:</strong> Unhandled exception at 0x00CD8391 in DobulyList.exe: 0xC0000005: Access violation writing location 0x00000004.</p>