| | |
using strtok()
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2009
Posts: 8
Reputation:
Solved Threads: 0
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..
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..
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.
c Syntax (Toggle Plain Text)
//get subsequent lines into a char buffer while (fgets(line,MAX_LINE_LEN,file_ptr)) { // tokenize the first item by comma delimiter line_ptr = strtok(line,","); while(line_ptr != NULL) { // if there's a decimal point, assume token is float (double) if (strstr(line_ptr,".")) floatArray[floatIndex++] = atof(line_ptr); // otherwise, it's asssumed to be an integer else intArray[intIndex++] = atol(line_ptr); // tokenize subsequent items by comma delimiter line_ptr = strtok(NULL,","); } }
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:
C Syntax (Toggle Plain Text)
int r, c; int arr[numRows][numCols]; while(/*there's another line*/) { for(c = 0; c < 4 /*num cols*/; c++) { arr[r][c] = /*data read*/; } r++; }
Last edited by death_oclock; Apr 2nd, 2009 at 11:30 pm.
•
•
•
•
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 ",".
i don't know, i'm just sayin'
.
Last edited by jephthah; Apr 3rd, 2009 at 1:11 am.
![]() |
Similar Threads
- Segmentation fault using strtok (C)
- problems with strtok (C++)
- strtok() function questions <C programming> (C)
- Strtok() (C)
- piglatin program with fgets, strtok, and strlen (C)
Other Threads in the C Forum
- Previous Thread: Inserting buttons in an Array
- Next Thread: Command line
| Thread Tools | Search this Thread |
adobe ansi api array arrays bash binarysearch centimeter char character convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km license linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string strings suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






