Parse values from a text file in C

Posted by Mohit Deshpande on Stack Overflow See other posts from Stack Overflow or by Mohit Deshpande
Published on 2010-04-18T19:37:15Z Indexed on 2010/04/18 19:43 UTC
Read the original article Hit count: 469

Filed under:
|
|
|

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!

© Stack Overflow or respective owner

Related posts about token

Related posts about textfiles