using strtok()

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 8
Reputation: swetharvss is an unknown quantity at this point 
Solved Threads: 0
swetharvss swetharvss is offline Offline
Newbie Poster

using strtok()

 
0
  #1
Apr 2nd, 2009
I have a file which contains data in this manner-
1, 20.6, 33, 45, 67
2, 58.9, 54, 644, 233
3, 67.2, 67, 345, 889
.
.
.
.

where the second column contains float data and the others are all integers. I need to read this file in my program and use the numbers seperately. And can i use strtok() function here to seperate the numbers delimiting by the ',' ? If anyone has a solution plz let me know..
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,607
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: using strtok()

 
0
  #2
Apr 2nd, 2009
this snippet does no error checking, and is full of assumptions, but I'm posting it here so you get an idea of how strtok() could work.

  1. //get subsequent lines into a char buffer
  2. while (fgets(line,MAX_LINE_LEN,file_ptr))
  3. {
  4. // tokenize the first item by comma delimiter
  5. line_ptr = strtok(line,",");
  6.  
  7. while(line_ptr != NULL)
  8. {
  9. // if there's a decimal point, assume token is float (double)
  10. if (strstr(line_ptr,"."))
  11. floatArray[floatIndex++] = atof(line_ptr);
  12.  
  13. // otherwise, it's asssumed to be an integer
  14. else
  15. intArray[intIndex++] = atol(line_ptr);
  16.  
  17. // tokenize subsequent items by comma delimiter
  18. line_ptr = strtok(NULL,",");
  19.  
  20. }
  21. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 8
Reputation: swetharvss is an unknown quantity at this point 
Solved Threads: 0
swetharvss swetharvss is offline Offline
Newbie Poster

Re: using strtok()

 
-1
  #3
Apr 2nd, 2009
Thanks for the code. But the main point here is to store each column values in the file in different arrays coz i have to use those stored array values further in the program. Since i need to store all the values in different arrays which are here in this case separated by a ",".
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: using strtok()

 
0
  #4
Apr 2nd, 2009
If you already know how to use strtok, why did you use that in your topic title? Well anyway, I think you want something like this:
  1. int r, c;
  2. int arr[numRows][numCols];
  3. while(/*there's another line*/)
  4. {
  5. for(c = 0; c < 4 /*num cols*/; c++)
  6. {
  7. arr[r][c] = /*data read*/;
  8. }
  9. r++;
  10. }
Last edited by death_oclock; Apr 2nd, 2009 at 11:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,607
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: using strtok()

 
0
  #5
Apr 3rd, 2009
Originally Posted by swetharvss View Post
Thanks for the code. But the main point here is to store each column values in the file in different arrays coz i have to use those stored array values further in the program. Since i need to store all the values in different arrays which are here in this case separated by a ",".
well... maybe if you would learn how to ask questions properly, you would have titled your question "using arrays" and asked how to index arrays, rather than ask how to use strtok().

i don't know, i'm just sayin'


.
Last edited by jephthah; Apr 3rd, 2009 at 1:11 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC