cannot read after \n using strtok()



Hi guys, I'm trying to figure out the problem with my C code below
implementing strtok(). I am trying to read in a CSV text file which
contains information separated by commas and \n newlines. In the CSV
file, the first line is filled with header info that I don't want to
store in my array. After the header info is the useful info, for
example:

loan_id,lender_id,borrower_id,principle_amt,interest_rate,loan_status,loan_balance,fco_discount,loan_amt_type,int_rate_type,risk,property_addr,property_city,property_st,property_zip,settlement_date
2445,44490,3332000,344123.22,5.9,default,301223.89,1.2,Regular,Fixed,
22,204 Main Street,Baltimore,MD,21076,Mar 30 2009
10446,1490,367000,246128.15,7.65,good,199009.43,3.8,regular,ARM,
12,23309 Pinetree Road,Birmingham,AL,78995,Sep 09 2012
433905,33,277705,566909.04,7.65,default,209880.43,3.8,Jumbo,ARM,33,905
Pacific Coast Highway,Malibu,CA,90254,Dec 01 2021

Each line of comma-separated info is separated by a \n character. Here
is my code:
**************************************************************************
filecontent_p = filecontent; //set the pointer to index 0 of array

filecontent_p = strtok(filecontent, "\n"); //skip
first line
printf("filecontent_p = %s\n", filecontent_p);

n=0;

filecontent_p = strtok(NULL, ",");
printf("filecontent_p = %s\n", filecontent_p);
newloan[n].loan_id = atoi(filecontent_p);
printf("loan[%i].loan_id = %i\n", n,
newloan[n].loan_id);

filecontent_p = strtok(NULL, ",");
printf("filecontent_p = %s\n", filecontent_p);
newloan[n].lender_id = atoi(filecontent_p);
printf("loan[%i].lender_id = %i\n", n,
newloan[n].lender_id);

AND ON AND ON...
****************************************************************************
filecontent_p is a pointer to my large array that will store the
information. I am able to read the first line containing the header
information using the first strtok() command. Any subsequent reads are
not possible. All the following filecontent_p reads after I read in
the header line result in NULLs. Where is the pointer pointing to that
it results in NULL reads? I thought it should point to the first
character of the next line that contains info. Any help would be
great. thanks!

.



Relevant Pages

  • 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)
  • 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)
  • Re: How to best parse a CSV data file and do a lookup in C?
    ... It, in fact, can parse out the complete CSV standard (which includes ... Using strtok() is rarely ever the right solution. ... You should use strcspn() instead -- it essentially provides the same ... has no effect on reenterability). ...
    (comp.lang.c)