parsing strings in c: what is the right way?

Discussion in 'Mac Programming' started by wala, Dec 26, 2006.

  1. wala macrumors member

    Jun 3, 2005
    What would be a normal way to, say, parse a string from a configuration file for a program written in C? Would it simply be a series of while loops, eg, in the case of getting past whitespace, something like:
    while(*string && isblank(*string))
    Or are there standard libraries and tools which should be used to parse strings besides functions like strtok()?
  2. bronxbomber92 macrumors regular

    Nov 23, 2006
    I would say these would provide the combination of functions (and good memory management) to do what you want (and more)
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <malloc.h>
  3. wala thread starter macrumors member

    Jun 3, 2005
    What is the current official recommendation regarding malloc.h? I haven't seen much code which uses that #include....
  4. iMeowbot macrumors G3


    Aug 30, 2003
    It almost seems as though there as many "right" ways to do this as there are programs, everyone feels the need to reinvent this stuff. Once upon a time this kind of made sense, but there are good standardised alternatives today.

    On the Mac, you can use property lists, all the hairy code is already written. On Windows, use the registry. On "plain" Unixy systems, there are lots of nice libraries around that will handle XML, LanMan-style .ini files, and many more (and those would be fine with the Mac and Windows too, if you want a single implementation).
  5. gnasher729 macrumors P6


    Nov 25, 2005
    That's not surprising, because malloc, calloc, realloc and free are included in stdlib.h.
  6. edm macrumors newbie

    Dec 27, 2006
    Austin, TX

    Seems like there would be a Regular Expressions library available in C that would handle all all your parsing needs.
  7. darkwing macrumors 65816

    Jan 6, 2004
    I agree. Use regex!

    On OSX, just type "man regex" at a terminal prompt
  8. wala thread starter macrumors member

    Jun 3, 2005
    How would I use regexec's substring matching feature? (I cannot seem to find understandable documentation for it)
    Examples would be nice....

Share This Page