int main() { char str[]="-This, is 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; }
void main(int argc, char **argv) { char buf[256]; int i; char *t; char token[8]; int tokenLen; if (argc < 3) { fprintf(stderr, "Usage: test \"buffer,with,delims\" <delimiters>\n"); exit(1); } strcpy(buf, argv[1]); i = 1; t = strtok(buf, argv[2]); if (t == NULL) exit(0); do { printf("strtok %d=[%s] length=%d\n", i, t, (int) strlen(t)); t = strtok(NULL, argv[2]); ++i; } while (t != NULL); printf("------------------------------------------------\n"); strcpy(buf, argv[1]); i = 1; t = Strtok(buf, argv[2]); if (t == NULL) exit(0); do { printf("Strtok %d=[%s] length=%d\n", i, t, (int) strlen(t)); t = Strtok(NULL, argv[2]); ++i; } while (t != NULL); printf("------------------------------------------------\n"); strcpy(buf, argv[1]); i = 1; tokenLen = Strntok(token, sizeof(token), buf, argv[2]); if (tokenLen < 0) exit(0); do { printf("Strntok %d=[%s] length=%d\n", i, token, tokenLen); tokenLen = Strntok(token, sizeof(token), NULL, argv[2]); ++i; } while (tokenLen >= 0); exit(0); }
/* ContactList constructor will read in a file, tokenize a string, create a contact instance and insert it in the binary tree. Pre: Post: Binary tree is populated and number_of_contacts holds the number of contacts in the file */ ContactList::ContactList() : number_of_contacts (0) { ifstream contact_input; string buffer, name_buffer, phone_buffer, web_buffer; contact_input.open("in.txt"); while (getline (contact_input, buffer)) { //I don't really know C++ string handling well and it shows Strtok(buffer, name_buffer, ","); Strtok(buffer, phone_buffer, ","); Strtok(buffer, web_buffer, ","); Contact contact_input (name_buffer, phone_buffer, web_buffer); //create a contact instance to input in the tree contacts.insert(contact_input); number_of_contacts++; } }
size_t Strtoks( char* str, const char* delim, char** toks, size_t ntoks) { StrtokState state; char* p; size_t n = 0; for (p = Strtok(&state, str, delim); p; p = Strtok(&state, NULL, delim)) { if (n == ntoks) return (size_t)-1; toks[n++] = p; } return n; }
char* StrtokTrim( StrtokState* state, char* str, const char* delim) { char* p = Strtok(state, str, delim); if (p) return StripOuterSpaces(p); return p; }
int Mkdirhier(const char* path_, int mode) { char path[PAL_MAX_PATH_SIZE]; char buf[PAL_MAX_PATH_SIZE]; char* p; char* context = NULL; /* Make a complete copy of the path (that we can destroy) */ if (Strlcpy(path, path_, sizeof(path)) >= sizeof(path)) return -1; buf[0] = '\0'; for (p = Strtok(path, "/", &context); p; p = Strtok(NULL, "/", &context)) { #if defined(CONFIG_OS_WINDOWS) /* Skip drive letters (on Windows) */ if (p == path && isalpha((unsigned char)p[0]) && p[1] == ':' && p[2] == '\0') { Strlcat(buf, p, sizeof(buf)); continue; } #endif /* Concatenate next component */ Strlcat(buf, "/", sizeof(buf)); Strlcat(buf, p, sizeof(buf)); /* Create directory if it does not already exist */ if (!Isdir(buf)) { if (Mkdir(buf, mode) != 0) return -1; } } return 0; }