home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / misc / htest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-30  |  1.5 KB  |  54 lines

  1. #include <stdio.h>
  2. #include <search.h>
  3.  
  4.  
  5. struct info {          /* this is the info stored in the table */
  6.      int age, room; /* other than the key. */
  7. };
  8. #define NUM_EMPL    5000    /* # of elements in search table */
  9. main( )
  10. {
  11.      /* space to store strings */
  12.      char string_space[NUM_EMPL*20];
  13.      /* space to store employee info */
  14.      struct info info_space[NUM_EMPL];
  15.      /* next avail space in string_space */
  16.      char *str_ptr = string_space;
  17.      /* next avail space in info_space */
  18.      struct info *info_ptr = info_space;
  19.      ENTRY item, *found_item, *hsearch( );
  20.      /* name to look for in table */
  21.      char name_to_find[30];
  22.      int i = 0;
  23.      /* create table */
  24.      (void) hcreate(NUM_EMPL);
  25.      while (scanf("%s%d%d", str_ptr, &info_ptr->age,
  26.               &info_ptr->room) !=
  27. EOF && i++ <
  28. NUM_EMPL) {
  29.             /* put info in structure, and structure in item */
  30.             item.key = str_ptr;
  31.             item.data = (char *)info_ptr;
  32.             str_ptr += strlen(str_ptr) + 1;
  33.             info_ptr++;
  34.             /* put item into table */
  35.             (void) hsearch(item,
  36. ENTER);
  37.      }
  38.      /* access table */
  39.      item.key = name_to_find;
  40.      while (scanf("%s", item.key) != EOF) {
  41.            if ((found_item = hsearch(item,
  42. FIND)) != NULL) {
  43.             /* if item is in the table */
  44.              (void)printf("found %s, age = %d, room = %d\n",
  45.              found_item->key,
  46.              ((struct info *)found_item->data)->age,
  47.              ((struct info *)found_item->data)->room);
  48.            } else {
  49.              (void)printf("no such employee %s\n",
  50.              name_to_find);
  51.            }
  52.      }
  53. }
  54.