I'm not sure why this glib hash table is unable to match the keys - I am curious to know why. I read a delimited file and insert it into the hash table, it can read the last value, but not the first or any before that.
Any help?
I was reading in a text file in the format:
001$002$Someval
and so on....
And the Code:
FILE *f = fopen(DEFAULT_MSG_CODES, "r");
/* If file does not exist return 1 */
if (f == NULL) {
return -1;
}
char *line;
char *token;
char *fields[8];
int i;
line = malloc(2048);
GHashTable* hash = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert(hash, "000", "test");
while (fgets(line, 2048, f) != NULL) {
line[strlen(line) - 1] = '\0';
i = 0;
token = strtok(line, "$");
while (token != NULL) {
fields[i] = token;
i++;
token = strtok(NULL, "$");
}
printf("cid: %s eid: %s msg %s\n",fields[0],fields[1],fields[2]);
g_hash_table_insert(hash,fields[0],fields[1]);
}
free(line);
fclose(f);
printf("There are %d keys in the hash\n", g_hash_table_size(hash));
printf("There is a match %s\n", g_hash_table_lookup(hash,"003"));