josilva00
josilva00

Reputation: 35

How can i save and read a linked list to/from a binary file?

So im making a program that involves multiple linked lists like the one bellow, what im trying to do is saving the nodes from a certain linked list, in case the program crashes, and the read the binary file so that i dont loose the nodes that id already added. I just dont know how to do this. i was thinking of doing a method like this

void LinkedList::saveOn(std::ostream& os) {
    Node* temp = head;
    while (temp != NULL) {
        os << temp->data;
        temp = temp->next;
    }
}

for each linked list that i have, but i not sure if this will work and how to read back from the file

class Node {
    public:
        int code;
        doble price;
        string name;
        Node* next;
    };

class LinkedList{
public:
    LinkedList() { // constructor
        head = NULL;
    }
    ~LinkedList() {}; // destructor
    void addNode(int val);
    void reverseList();
    void display();
private:
    Node* head;
};

// function to add node to a list
void LinkedList::addNode(int val) {
    Node* newnode = new Node();
    newnode->data = val;
    newnode->next = NULL;
    if (head == NULL) {
        head = newnode;
    }
    else {
        Node* temp = head; // head is not NULL
        while (temp->next != NULL) { 
            temp = temp->next; // go to end of list
        }
        temp->next = newnode; // linking to newnode
    }
}

// reverse Linked List
void LinkedList::reverseList() {
    Node* temp = head;
    Node* nextnode = NULL;
    Node* revnode = NULL;
    while (temp != NULL) {
        head = temp;
        nextnode = temp->next;
        temp->next = revnode;
        revnode = temp;
        temp = nextnode;
    }
}

void LinkedList::display() {
    if (head == NULL) {
        cout << "List is empty!" << endl;
    }
    else {
        Node* temp = head;
        while (temp != NULL) {
            cout << temp->data << " ";
            temp = temp->next;
        }
        cout << endl;
    }
}

Upvotes: 0

Views: 1222

Answers (1)

Henry Le Berre
Henry Le Berre

Reputation: 920

NOTE: This solution requires an int to be of constant size across all machines you run this code on.

Since your "Node" object is a fixed size, you should be able to just write the raw data (i.e int) to a file (without any spaces). The "next" pointer doesn't have to be saved since it will change when you recreate the linked list from the file.

When you want to recreate the linked list, simply read the whole file to a buffer and keep creating new elements while you haven't reached the end of the buffer.

Example: To Write:

Node* current = head;
FILE* fp = fopen("save.bin", "wb");

while (current != NULL) {
    fwrite(&current->data, sizeof(int), 1, fp);
    current = current->next;
}

fclose(fp);

To Read:

FILE* fp = fopen("save.bin", "rb");

int newData;
while (fread(&newData, sizeof(int), 1, fp) > 0) {
    addNode(newData);
}

fclose(fp);

Now that I know that you want to store strings, you can store each node as a single line you can do the following:

To Write:

Node* current = head;
FILE* fp = fopen("save.bin", "wb");

while (current != NULL) {
    fwrite(&current->code, sizeof(int), 1, fp);
    fwrite(&current->price, sizeof(double), 1, fp);
    
    // The string (plus the null byte)
    fwrite(current->name.c_str(), sizeof(char), current->name.length() + 1, fp);
    // newline to indicate new node
    if (current->next != NULL)
        fprintf(fp, "\n");

    current = current->next;
}

fclose(fp);

Upvotes: 1

Related Questions