Tools.h++ Manual

104011 Tandem Computers Incorporated 10-3
10
The following example shows the use of class
RWFileManager
to construct a
linked-list of ints on disk: The source code is included in the
toolexam
subdirectory as
example7.cc
and
example8.cc
.
Code Example 10-1
#include <rw/filemgr.h> // 1
#include <rw/rstream.h>
struct DiskNode { // 2
int data; // 3
RWoffset nextNode; // 4
};
main()
{
RWFileManager fm("linklist.dat"); // 5
// Allocate space for offset to start of the linked list:
fm.allocate(sizeof(RWoffset)); // 6
// Allocate space for the first link:
RWoffset thisNode = fm.allocate(sizeof(DiskNode)); // 7
fm.SeekTo(fm.start()); // 8
fm.Write(thisNode); // 9
DiskNode n;
int temp;
RWoffset lastNode;
cout << “Input a series of integers, “;
cout << “then EOF to end:\n”;
while (cin >> temp) { // 10
n.data = temp;
n.nextNode = fm.allocate(sizeof(DiskNode)); // 11
fm.SeekTo(thisNode); // 12
fm.Write(n.data); // 13
fm.Write(n.nextNode);
lastNode = thisNode; // 14
thisNode = n.nextNode;
}
fm.deallocate(n.nextNode); // 15
n.nextNode = RWNIL; // 16
fm.SeekTo(lastNode);
fm.Write(n.data);