简体   繁体   中英

How to write a linked list object to a file

void writeFile(){
    Employer *temp = head;
    while (temp != NULL)
    {
        temp->tryWrite();//Employee's display method called here
        temp = temp->getNext();
    }
}

void main(){
    EmployerList em;
    em.AddNode("des@yahoo.com", "LIME", "Manager", "ming", "ding", "Newston", "43", "873");
    em.AddNode("sw@gmail.com", "NOKIA", "CEO", "rew", "nbv", "Europe", "0411-789-6548", "985-257-1111"); 
    //em.writeFile();
}

I am trying to write the linked list to the file as an object instead of singularly like i have in the tryWrite function. any help?

The short answer is "No, you can't do that."

A linked list, pretty much by definition, contains pointers. At least for long-term storage (ie, anything that survives outside a single execution of the program), you can't get anything meaningful by storing a pointer to disk, and then reading it back in.

Serializing data structures that contain pointers generally requires "chasing" those pointers, and "flattening" the structure in some way. For a linear linked list, you probably just want to write the nodes in order. Another possibility is to substitute offsets in the file for pointers to memory. That's primarily useful if you need to maintain the same type of structure on disk (eg, you want a tree that you can manipulate on disk as an actual tree, not just a sequence of records).

Anyway you go at it, though, just storing the raw data is unlikely to be useful.

If you mean to store the linked list to disk exactly the same as what's stored in memory, you cannot. Because the pointers in linked list are "volatile", the things they're pointing to can be very different, when you run your application in different time. This means it's meaningless to persist the pointers themselves.

But you can persist the nodes in a linked list, as well as their relationships(which is maintained previously by pointers) to disk. If you're using a single linked list, just store the nodes one by one, from head to end.

The code you show is incomplete and does not compile. You are missing declarations for the class member variables. Your constructor for EmployerList should probably be taking a const Employer & and storing that; it should not be dealing with all the variables used to initialize an Employer.

Writing a linked list to file is modestly tricky. You can't meaningfully write pointers; you have to write the data values for each item in a standardized, readily readable format. You use proximity in the file to indicate 'next' (and 'prev') relationships. This process is called 'serialization' on output and 'deserialization' on input.


This idiom:

    Employer *node = new Employer(Email, cName, pos, fName, lName, addr, tHome, tMobile);  

    //if memory was sucessfully allocated
    if (node != NULL)

is archaic (pre-standard, meaning pre-1998 standard) C++. You didn't invoke a 'no throw' allocator; if the allocation failed, an exception was thrown. Therefore the allocation check is redundant.

This is not causing direct problems; the test is just wasteful (and indents your code more than necessary). If your text book uses this technique, you should probably get a newer text book.

(I also note that I had to fix up the spacing to make the code consistent; consistency in programming is very important.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM