Search code examples
c++linked-listbinaryfiles

save and load data from linked list to a binary file c++


I made a classic linked list that simulates an array that can get any kind of type you want. Of course you can delete and add organs and all methods work fine.

Now I have to write methods for writing and reading from a binary file and I do not really know what to do I've tried all sorts of ways to write to a file, and so far I have not been able to get anywhere and I do not even know what's wrong with the code,

thats part of my linked list code

    template <class T>
class LinkedList
{
private:
    struct node
    {
        T data;
        node * next;
    };
    node* head;

public:
    LinkedList();
    ~LinkedList();

    void add(T value);
    int find(T value);
    bool remove(T value);
    void clear();
    bool ifEmpty() const;
    int size() const;
    void display() const;
    T getData(int index) const;

    T &operator[](int index);


    void save(ofstream& out);
};

and thats the code of the save method that dosent work

template <class T>
void LinkedList<T>::save(ofstream& out)
{
    node* temp = head;
    T temp2 = temp->data;

    for (int i = 0; i < size(); i++)
    {
        out.write((const char*)(temp2), sizeof(temp2));
        temp = temp->next;
        temp2 = temp->data;
    }
}

please if some of you pros can give me a direction of how to do it right...


Solution

  • You need to write the size of the list first. This will allow you to read the list. Without reading the size, there is no way for you to decide when to stop reading from the file.

    Also, you have an error. The line

            out.write((const char*)(temp2), sizeof(temp2));
    

    needs to be

            out.write((const char*)(&temp2), sizeof(temp2));
                                    ^^ Missing
    

    template <class T>
    void LinkedList<T>::save(ofstream& out)
    {
       size_t sz = size();
       out.write((const char*)(&sz), sizeof(sz));
    
       node* temp = head;
       T temp2 = temp->data;
    
       for (int i = 0; i < size(); i++)
       {
          out.write((const char*)(&temp2), sizeof(temp2));
          temp = temp->next;
          temp2 = temp->data;
       }
    }
    

    Potential read implementation.

    template <class T>
    void LinkedList<T>:read(ifstream& in)
    {
       size_t sz = 0;
       in.read((char*)(&sz), sizeof(sz));
    
       // Make sure that list is empty
    
       for ( size_t i = 0; i < sz; ++i )
       {
          T temp;
          in.read((char*)(&temp), sizeof(temp));
    
          // Add temp to the list.
          add(temp);
       }
    }
    

    An updated version of save that should fix the null pointer access problem if size() returns the correct value.

    template <class T>
    void LinkedList<T>::save(ofstream& out)
    {
       size_t sz = size();
       out.write((const char*)(&sz), sizeof(sz));
    
       node* temp = head;
       for (int i = 0; i < size(); i++)
       {
          // Pull the data out of the pointer here.
          // At this point, temp should not be NULL.
          T temp2 = temp->data;
          out.write((const char*)(&temp2), sizeof(temp2));
          temp = temp->next;
       }
    }