#include <search.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define HASHT_SIZE 2000000
#define N 1000000
char *keys[N];
int keySize;
int main() {
ENTRY e, *ep;
hcreate(HASHT_SIZE);
FILE *f;
f = fopen("./map.txt", "r");
char line[500], *li;
double dub;
while (fgets(line, sizeof(line), f)) {
e.key = line;
if (hsearch(e, FIND) == NULL) {
e.data = (void *)keySize;
li = malloc(strlen(e.key) * sizeof(char));
strcpy(li, line);
ep = hsearch(e, ENTER);
keys[keySize++] = li;
if (ep == NULL) {
printf("failure");
exit(1);
}
}
}
}
我的问题是,运行下一个hsearch(e,FIND)
时,通过NULL
进行的查找将返回while(fgets(line,sizeof(line),f))
。这是为什么?为什么hsearch(keys[0],FIND)
一被调用fgets
就会失败?调用fgets
时,哈希表如何忘记该条目?