what are tokens and what does strtok do?
tokens
Collapse
X
-
strtok tells how you want to separate a string and where you want to store it.
Heres a basic example.
if the first line of text said "this is, the line of, text separated by a specific token"Code:FILE *f_ptr; //points to an open text file f_ptr = fopen("MyText.txt", "rt"); //opens the text file if(f_ptr == NULL) //error opening file return; char * c_Ptr = NULL; // holds a temparay selection sections char c_Ptr1[1000] = {NULL}; //holds the entire line of text for(;fgets(c_Ptr1, 1000, f_ptr)!=NULL;) //grabs the next line of text { c_Ptr = strtok(c_Ptr1, ","); // first time to separate hold the location in the line while (c_Ptr != NULL) c_Ptr = strtok(NULL, ","); }
the first time strtok c_Ptr = this is
2nd strtok c_Ptr = the line of
3rd strtok c_Ptr = text separated by a specific token
Then when it hits the for loop it grabs the next line in the text file until it find the end of file.Comment
-
Wow my example is pretty ugly here is a much cleaner example fro www.cplusplus.c om
Output:Code:/* strtok example */ #include <stdio.h> #include <string.h> int main () { char str[] ="- This, a sample string."; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " ,.-"); } return 0; }
Splitting string "- This, a sample string." into tokens:
This
a
sample
stringComment
Comment