Tools.h++ Manual
104011 Tandem Computers Incorporated 10-5
10
14. Since we allocate the next link before we write the current link, the final
link in the list will have an offset to an allocated block that is not used. It
must be handled as a special case.
15. First, deallocate the final unused block.
16. Next, reassign the offset of the final link to be
RWNIL
. When the list is
read, this will indicate the end of the linked list. Finally, re-write the
final link, with the correct information.
17. The destructor for class
RWFileManager
, which closes the file, will be
called here.
Having created the linked-list on disk, how might we read it? Here is a
program that reads the list and prints the stored integer field.
#include <rw/filemgr.h>
#include <w/rstream.h>
struct DiskNode {
int data;
RWoffset nextNode;
};
main()
{
RWFileManager fm("linklist.dat"); // 1
fm.SeekTo(fm.start()); // 2
RWoffset next;
fm.Read(next); // 3
DiskNode n;
while (next != RWNIL) { // 4
fm.SeekTo(next); // 5
fm.Read(n.data); // 6
fm.Read(n.nextNode);
cout << n.data << "\n"; // 7
next = n.nextNode; // 8
}
return 0;
} // 9