This shows you the differences between two versions of the page.
c:string.h:strtok [2012/07/16 22:41] 127.0.0.1 external edit |
c:string.h:strtok [2024/02/16 01:06] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | char *strtok(char *str1, const char *str2); | + | {{keywords>wiki library source code example reference}} |
+ | ====== strtok ====== | ||
+ | |||
+ | <code c> | ||
+ | #include <stdlib.h> | ||
+ | char *strtok(char *str1, const char *str2); | ||
+ | </code> | ||
+ | ===== description of strtok ===== | ||
+ | Function strtok breaks a string (str1) into individual strings based on the so-called token. The string is separated by one delimiter (str2). | ||
+ | "str2" may contain multiple tokens, e.g. str2 = "\ n" (ie separation in space, comma, New Line, Point). | ||
+ | |||
+ | ===== strtok C Sourcecode Example ===== | ||
+ | <code c> | ||
+ | /* | ||
+ | * strtok example code | ||
+ | * http://code-reference.com/c/string.h/strtok | ||
+ | */ | ||
+ | #include <stdio.h> | ||
+ | #include <string.h> | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | // this example shows how you can easy parse a csv file in c | ||
+ | char csvcontent[] = "row;of a csv file; with cool content;parse csv in c"; | ||
+ | char seperator[] = ";"; | ||
+ | char *string; | ||
+ | int i=1; | ||
+ | string = strtok(csvcontent, seperator); | ||
+ | |||
+ | while(string != NULL) { | ||
+ | printf("column %d = %s\n", i++, string); | ||
+ | string = strtok(NULL, seperator); | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </code> | ||
+ | |||
+ | ==== output of strtok c example ==== | ||
+ | column 1 = row | ||
+ | column 2 = of a csv file | ||
+ | column 3 = whith cool content | ||
+ | column 4 = parse csv in c | ||
+ |