Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: How to parse this string into a list structure?

Thread Tools

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

Barry Schwarz
Posts: n/a
On Fri, 07 Nov 2008 02:36:04 +0000, "(2b|!2b)==?"
<(E-Mail Removed)> wrote:

>I am expecting a string of this format:
>"id1aram1,param2;id2aram1,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 ;
>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
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to parse a string completely into a list Python 7 09-25-2008 02:45 PM
is there any faster way to parse string into float number C++ 9 03-17-2006 10:22 AM
How do I parse a string into individual characters? (really simple!) really! Jeannie C++ 15 08-30-2005 08:34 AM
How to parse a string like C program parse the command line string? C Programming 19 03-15-2005 07:41 PM