Re: How to parse this string into a list structure?

Discussion in 'C Programming' started by Barry Schwarz, Nov 7, 2008.

  1. On Fri, 07 Nov 2008 02:36:04 +0000, "(2b|!2b)==?"
    <> wrote:

    >I am expecting a string of this format:
    >
    >"id1:param1,param2;id2:param1,param2,param3;id"
    >
    >The tokens are seperated by semicolon ";"
    >
    >However each token is really a struct of the following format:
    >
    >struct mst_
    >{
    > int id;
    > struct params_* parms ; //0 or more
    > struct mst_ *next ;
    >};
    >
    >
    >where:
    >
    >struct params_
    >{
    > float argval;
    > struct params_ * next;
    >};


    Try to remember to put the question ( How to parse this string into a
    list structure?) in your text, not just in your subject.

    With regard to parsing: If you are ABSOLUTELY certain all the strings
    are well formed and if the string is modifiable, you could use strtok.
    If either of the previous conditions is false, or if you think you
    might need to reprocess the string, or if you just don't like strtok,
    you could use strchr to find your separators and sscanf to extract the
    fields of interest. If there is a possibility that some of the fields
    will contain invalid data, you might want to convert each field into a
    string and use the appropriate strto_ functions which will let you
    check for errors. (Not part of your question but why is argval a
    float and not a double?)

    With regard to the linked list: If you KNOW the maximum number of ids
    and the maximum number of total parameters, you could define an array
    of struct mst_ and an array of struct params_, each with the required
    number of elements, and set the pointers to the address of each
    element as appropriate. If you don't know the maximums or if the
    numbers are potentially larger than you want to define arrays for, you
    can use malloc to allocate space for each structure as you decide you
    have something to store in it. In either case, algorithms for
    chaining nodes to a linked list are mostly language independent
    (consequently slightly off topic) and can be found in almost any data
    structures text. Implementations of these algorithms have been
    discussed repeatedly in this newsgroup (google is at least one source
    of archived messages).

    --
    Remove del for email
     
    Barry Schwarz, Nov 7, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leo Nunez
    Replies:
    3
    Views:
    1,303
    Neil Kurzman
    Feb 9, 2005
  2. Replies:
    19
    Views:
    1,189
    Daniel Vallstrom
    Mar 15, 2005
  3. Replies:
    7
    Views:
    290
  4. lg
    Replies:
    3
    Views:
    327
    Tad McClellan
    Aug 9, 2007
  5. Replies:
    2
    Views:
    294
    Rui Maciel
    Dec 12, 2012
Loading...

Share This Page