Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8367539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:11:07+00:00 2026-06-09T13:11:07+00:00

I’m not sure why this glib hash table is unable to match the keys

  • 0

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"));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:11:09+00:00Added an answer on June 9, 2026 at 1:11 pm

    If I’m not mistaken, you’re clobbering the data you put into the hash table on the next iteration of the loop by overwriting line with a new call to fgets. An easy fix would be to strdup (or g_strdup or whatever the ugly glib name for it is) the strings before you add them to the hash table. But you should really read the glib documentation on this API to find out the ownership contract for the data you pass in, and whether the arguments are just pointers to data it will immediately read, or pointers to objects that will become a permanent part of the hash table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I know there's a lot of other questions out there that deal with this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.