Re: How to best parse a CSV data file and do a lookup in C?

From: Paul Hsieh (websnarf_at_gmail.com)
Date: 11/20/04


Date: 19 Nov 2004 15:58:40 -0800

Robert Gamble <rgamble99@gmail.com> wrote:
> On Thu, 18 Nov 2004 23:48:46 -0500, Robert Gamble wrote:
> > On Thu, 18 Nov 2004 20:23:24 -0800, Johnny Google wrote:
> >> The main point here is I need help with either an already created
> >> library which handles data files like this - or some C equivalent to
> >>
> >> @data = split /,/, $line;
> >>
> >>
> >> Since I think I know how to read the contents, and loop through the
> >> array in C - and do the strcmp to test if it matches - I just don't
> >> know how to simply put break the line into an array by telling it what
> >> the delimiter is (as in the above split example).

Ok, Johnny Google. I've written a usable CSV parser here:

    http://www.pobox.com/~qed/bcsv.zip

It, in fact, can parse out the complete CSV standard (which includes
quoting so you can put a commas, lead/trailing spaces and in fact
quotes themselves in any given field) and thus should easily be able
to handle that case you are concerned with.

> > The only thing provided by Standard C for this purpose is strtok which
> > will probably work fine for your needs. Take a look at that and come back
> > if you have any questions about it.

Using strtok() is rarely ever the right solution. strtok() includes a
hidden side effect which makes using it in a reenterable way
impossible. Correct CSV parsing, in fact, does *NOT* reduce to a
simple line-by-line, then strtok with a "," seperator (though it may
match the sample data given by the OP.)

You should use strcspn() instead -- it essentially provides the same
functionality as strtok() without its negative side effects (strcspn()
has no effect on reenterability).

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/


Relevant Pages

  • Re: How to best parse a CSV data file and do a lookup in C?
    ... I've written a usable CSV parser here: ... > Using strtok() is rarely ever the right solution. ... > functionality as strtokwithout its negative side effects ... The strcspn function can be used to write a strtok-like ...
    (comp.lang.c)
  • cannot read after using strtok()
    ... In the CSV ... the first line is filled with header info that I don't want to ... information using the first strtok() command. ... Where is the pointer pointing to that ...
    (comp.lang.c)
  • Re: cannot read after using strtok()
    ... In the CSV ... the first line is filled with header info that I don't want to ... store in my array. ... information using the first strtok() command. ...
    (comp.lang.c)