There are a few errors in your code. First, char delims[] = " ", "\n"; is invalid and your compiler should have told you so. You probably want something like char * delims = " \n";
Also, if (delims = " ") is an assignment statement and in your code the compiler should warn or error about this as well. You can not compare character arrays in C with the == operator, you must use strcmp or an equivalent mechanism. That aside, you really dont need to check (or set) the value of delims within the loop body as it is not modified by the call to strtok .
One other thing, be aware that strtok modifies the searched string. If you want to store these values for later use you will need to make a copy to permanent memory.
L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
Personally, I'd use fgets() to get the whole line of text from the process file:
fgets(charBufferName, sizeof(charBufferName), filePointerName);
which easily gets 1 line of data, and in a while loop, gets every line of data:
int i=0;
while((fgets(buffer, sizeof(buffer), fpName)) != NULL) {
//then use sscanf() to get the data into your arrays:
sscanf(buffer, "%s %s %s ", array1[i],array2[i],array3[i]);
++i;
}
strtok() is fine, but you always have to watch out for what you can't see - that it changes the string it's tokenizing.
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185