User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:string.h:strtok

strtok

#include <stdlib.h>
char *strtok(char *str1, const char *str2);

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

/* 
 * 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;
}

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
  
advertising
c/string.h/strtok.txt · Last modified: 2013/02/23 18:10 by 158.181.88.241

Impressum Datenschutz