1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#include "uthash.h"
#include <stdlib.h> /* malloc */
#include <errno.h> /* perror */
#include <stdio.h> /* printf */
#define BUFLEN 20
#if 0
#undef uthash_expand_fyi
#define uthash_expand_fyi(tbl) printf("expanding to %d buckets\n", tbl->num_buckets)
#endif
typedef struct name_rec {
char boy_name[BUFLEN];
UT_hash_handle hh;
} name_rec;
int main()
{
name_rec *name, *names=NULL;
char linebuf[BUFLEN];
FILE *file;
int i=0,j=0;
file = fopen( "test14.dat", "r" );
if (file == NULL ) {
perror("can't open: ");
exit(-1);
}
while (fgets(linebuf,BUFLEN,file) != NULL) {
i++;
name = (name_rec*)malloc(sizeof(name_rec));
if (name == NULL) {
exit(-1);
}
strcpy(name->boy_name, linebuf);
HASH_ADD_STR(names,boy_name,name);
}
fseek(file,0L,SEEK_SET);
while (fgets(linebuf,BUFLEN,file) != NULL) {
HASH_FIND_STR(names,linebuf,name);
if (!name) {
printf("failed to find: %s", linebuf);
} else {
j++;
}
}
fclose(file);
printf("lookup on %d of %d names succeeded\n", j, i);
return 0;
}
|