943,787 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2367
  • C RSS
Apr 2nd, 2009
0

using strtok()

Expand Post »
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..
Similar Threads
Reputation Points: 9
Solved Threads: 0
Newbie Poster
swetharvss is offline Offline
8 posts
since Jan 2009
Apr 2nd, 2009
0

Re: using strtok()

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. }
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Apr 2nd, 2009
-1

Re: using strtok()

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 ",".
Reputation Points: 9
Solved Threads: 0
Newbie Poster
swetharvss is offline Offline
8 posts
since Jan 2009
Apr 2nd, 2009
0

Re: using strtok()

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.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Apr 3rd, 2009
0

Re: using strtok()

Click to Expand / Collapse  Quote originally posted by swetharvss ...
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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Inserting buttons in an Array
Next Thread in C Forum Timeline: Command line





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC