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

  • Home
  • SEARCH
  • 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 745219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:04:09+00:00 2026-05-14T09:04:09+00:00

Possible Duplicate: Parsing text in C Say I have written to a text file

  • 0

Possible Duplicate:
Parsing text in C

Say I have written to a text file in this format:

key1/value1
key2/value2
akey/withavalue
anotherkey/withanothervalue

I have a linked list like:

struct Node
{
    char *key;
    char *value;
    struct Node *next;
};

to hold the values. How would I read key1 and value1? I was thinking of putting line by line in a buffer and using strtok(buffer, ‘/’). Would that work? What other ways could work, maybe a bit faster or less prone to error? Please include a code sample if you can!

  • 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-05-14T09:04:10+00:00Added an answer on May 14, 2026 at 9:04 am

    Since your problem is a very good candidate for optimizing memory fragmentation, here is an implementation that uses some simple arcane magic to allocate all strings and the structure itself in a single piece of memory.

    When destroying the node, you need only a single call to free(), to the node itself.

    struct Node *list = NULL, **nextp = &list;
    char buffer[1024];
    
    while (fgets(buffer, sizeof buffer, file) != NULL) {
        struct Node *node;
    
        node = malloc(sizeof(struct Node) + strlen(buffer) + 1);
        node->key = strtok(strcpy((char*)(node+1), buffer), "/\r\n");
        node->value = strtok(NULL, "\r\n");
        node->next = NULL;
        *nextp = node;
        nextp = &node->next;
    }
    

    Explanation:

    With 20 comments and one unexplained downvote, I think that the code needs some explanation, specially with regards to the tricks employed:

    1. Building a linked list:

      struct Node *list = NULL, **nextp = &list;
      ...
      *nextp = node;
      nextp = &node->next;
      

      This is a trick to create a linked list iteratively in forward order without having to special-case the head of the list. It uses a pointer-to-pointer to the next node. First the nextp pointer points to the list head pointer; in the first iteration, the list head is set through this pointer-to-pointer and then nextp is moved to the next pointer of that node. Subsequent iterations fill the next pointer of the last node.

    2. Single allocation:

      node = malloc(sizeof(struct Node) + strlen(buffer) + 1);
      node->key = ... strcpy((char*)(node+1), buffer) ...
      

      We have to deal with three pointers: the node itself, the key string and the value string. This usually would require three separate allocations (malloc, calloc, strdup…), and consequently free separate releases (free). Instead, in this case, the spaces of the tree elements are summed in sizeof(struct Node) + strlen(buffer) + 1 and passed to a single malloc call, which returns a single block of memory. The beginning of this block of memory is assigned to node, the structure itself. The additional memory (strlen(buffer)+1) comes right after the node, and it’s address is obtained using pointer arithmetic using node+1. It is used to make a copy of the entire string read from the file (“key/value\n”).

      Since malloc is called a single time for each node, a single allocation is made. It means that you don’t need to call free(node->key) and free(node->value). In fact, it won’t work at all. Just a single free(node) will take care of deallocating the structure and both strings in one block.

    3. Line parsing:

      node->key = strtok(strcpy((char*)(node+1), buffer), "/\r\n");
      node->value = strtok(NULL, "\r\n");
      

      The first call to strtok returns the pointer to the beginning of the buffer itself. It looks for a ‘/’ (additionally for end-of-line markers) and breaks the string there with a NUL character. So the “key/value\n” is broken in “key” and “value\n” with a NUL character in between, and a pointer to the first is returned and stored in node->key. The second call to strtok will work upon the remaining “value\n”, strip the end-of-line marker and returning a pointer to “value”, which is stored in node->value.

    I hope this cleans all questions about the above solution… it is too much for a closed question. The complete test code is here.

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

Sidebar

Related Questions

No related questions found

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.