Re: cannot read after \n using strtok()



On Sun, 04 Nov 2007 00:24:30 -0000, "ohaqqi@xxxxxxxxx"
<ohaqqi@xxxxxxxxx> wrote:

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

I copied your code into a main function and initialized the array as
you described. All three calls to strtok worked as expected,
producing one long string of text and two short strings of digits.
Your problem has to be in something you are not showing us.

it results in NULL reads? I thought it should point to the first

strtok did not return NULL to me. Are you sure your array contains
all the lines you think it does? In my test I just initialized it
with the data you provided using adjacent string literals and adding
in the implied '\n' whenever a ',' was missing. I expect in your code
you are reading the data from a file. Why don't you print out the
entire array before the first call to strtok to be sure you have
everything.

character of the next line that contains info. Any help would be

On the first call to strtok, the return value points to the beginning
of the array. On a subsequent call, it points to the first
non-delimiter following the delimiter which terminated the previous
call.

great. thanks!


Remove del for email
.



Relevant Pages

  • 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: problem parsing strings
    ... You are probably better off using strtol or strtoul here instead of strtok ... Make a copy of constant strings into a local array (possibly dynamically ... they are safe to use with constant strings. ...
    (comp.lang.c)
  • Re: Strings and arrays
    ... words and save them into an array. ... The plan might have been to point to each word returned by strtok. ... I would copy the string counting words as I go. ... an array of pointers big enough for the job (we now ...
    (comp.lang.c)
  • Re: Strings and arrays
    ... words and save them into an array. ... The plan might have been to point to each word returned by strtok. ... In both cases the initial call to strcpy seems wrong. ... I would copy the string counting words as I go. ...
    (comp.lang.c)
  • Re: Two-Dimensional Char Array
    ... > I'm having problems where I need to parse a command line, ... > each token from strtok() into an array. ...
    (comp.lang.c)