#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);
}
}
}
}
My problem is that lookups via hsearch(e,FIND)
are returning NULL
as soon as the next while(fgets(line,sizeof(line),f))
is run. Why is that? Why does hsearch(keys[0],FIND)
fail as soon as this fgets
is called? How is the hash table forgetting the entry when fgets
is called?