CS360: Exam 1: 10/22/97. Answer to Question 4

Part 1

This code reads from the file inputfile. Inputfile is a file that contains names of people followed by their social security numbers. The format of inputfile is as follows: Each entry consists of an integer (stored as a raw integer) that is the number of characters in the name. Then it contains the name, not null-terminated, and the social security number, which is exactly 11 bytes (again, not null-terminated). Thus, if a person's name is n characters, the entry for that person will have n+15 bytes. It reads in all entries and then prints out one line per entry. Each line starts with the social security number, then a space, and then the person's name. The output is sorted by name.

Part 2

The code is inefficient becuase in each entry it makes three system calls (read). It may be made more efficient by adding buffering. The simplest way to do this is to use the stdio calls:
#include "rb.h"
#include < stdio.h >

main()
{
   FILE *f;
   Rb_node names, tmp;
   int i;
   char *name;
   char *ssn;

   names = make_rb();
   f = fopen("inputfile", "r");
   if (f == NULL) { perror("inputfile"); exit(1); }

   while (fread(&i, sizeof(int), 1, f) == 1) {
     name = (char *) malloc(sizeof(char)*(i+1));
     fread(name, 1, i, f);
     name[i] = '\0';
     ssn = (char *) malloc(sizeof(char)*(12));
     fread(ssn, 1, 11, f);
     ssn[11] = '\0';
     rb_insert(names, name, ssn);
   }
   rb_traverse(tmp, names) {
     printf("%s %s\n", tmp->v.val, tmp->k.key);
   }
}