Tools.h++ Manual

104011 Tandem Computers Incorporated 11-3
11
11.2 Example
In this example, key-value pairs of character strings and offsets to RWDates,
representing birthdays, are stored. Given a name, a birthdate can be retrieved
from disk.
Here's the line-by-line description:
1. Construct a BTree. The default constructor is used, resulting in a key
length of 16 characters (the default).
2. Read the name from standard input. This loop will exit when EOF is
reached.
3. Read the corresponding birthday.
4. Allocate enough space from the
RWFileManager
to store the birthday.
Member function
binaryStoreSize()
is a member function that most
Tools.h++ classes have. It returns the number of bytes necessary to store
an object in a binary file.
#include <rw/disktree.h>
#include <rw/filemgr.h>
#include <rw/cstring.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>
main()
{
RWCString name;
RWDate birthday;
RWFileManager fm(“birthday.dat”);
RWBTreeOnDisk btree(fm); // 1
while (cin >> name) // 2
{
cin >> birthday; // 3
RWoffset loc = fm.allocate(birthday.binaryStoreSize()); // 4
fm.SeekTo(loc); // 5
fm << birthday; // 6
btree.insertKeyAndValue(name, loc); // 7
}
return 0;
}