LINK LIST

Hi. i can store a linked list to a file but, i have problem for read . I don'n know how to start for reading
Use the ifstream class for reading files.
Can you show us the code you use to write to the file?
Hi Galik .

ofstream outFile("ABS.txt");

void Node::saveNode(Node* head, ofstream& outFile)
{
for (Node* cur = head ; cur != NULL; cur= cur->next)
{
outFile<< cur->data << endl;
}
}

this code work correctly and create file
Then assuming that data is a built-in type and/or has its input output operators correctly defined, something like this should work. You need to substitute the right type for your data and whatever the function names you use to clear the list and add new elements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ifstream inFile("ABS.txt");

void Node::loadNode(ifstream& inFile)
{
    clear(); // clear current data from list

    int data; // whatever type data is

    while(infile >> data) // try to read data from file
    {
        // read was successful so put data into list using regular methods
        append(data);
    }
}
Last edited on
Thanks Galikkk
Topic archived. No new replies allowed.