strtok

Strtok usage, code not working [duplicate]

牧云@^-^@ 提交于 2019-12-31 07:32:46
问题 This question already has answers here : strtok behavior (2 answers) Closed 6 years ago . I am trying to use strtok() . Following is the piece of code that I wrote. It does not work but prints ", '" infinitely. #include<stdio.h> #include<stdlib.h> #include<string.h> int main(){ char str[]="this, by the way, is a 'sample'"; char *tokens; tokens = strtok(str, ", '"); //printf("%s\n",tokens); //printf("%s\n", str); while(tokens!=NULL) { printf("%s\n", tokens); tokens = (NULL, ", '"); } return 0;

Strtok usage, code not working [duplicate]

放肆的年华 提交于 2019-12-31 07:32:24
问题 This question already has answers here : strtok behavior (2 answers) Closed 6 years ago . I am trying to use strtok() . Following is the piece of code that I wrote. It does not work but prints ", '" infinitely. #include<stdio.h> #include<stdlib.h> #include<string.h> int main(){ char str[]="this, by the way, is a 'sample'"; char *tokens; tokens = strtok(str, ", '"); //printf("%s\n",tokens); //printf("%s\n", str); while(tokens!=NULL) { printf("%s\n", tokens); tokens = (NULL, ", '"); } return 0;

strtok with space delimiter [closed]

拥有回忆 提交于 2019-12-31 03:41:09
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . hey i am trying to use the strtok function in C with " " as a delimiter and for some reason it does not work. can some one please tell me how to parse using strtok with a space as a delimiter thanks in advance 回答1: Stolen (and slightly modified) from here. /* strtok example */ #include <stdio.h> #include <string

strtok_s behaviour with consecutive delimiters

*爱你&永不变心* 提交于 2019-12-30 11:03:29
问题 I'm parsing 3 values in parallel which are separated with a specific separator. token1 = strtok_s(str1, separator, &nextToken1); token2 = strtok_s(str2, separator, &nextToken2); token3 = strtok_s(str3, separator, &nextToken3); while ((token1 != NULL) && (token2 != NULL) && (token3 != NULL)) { //... token1 = strtok_s(NULL, separator, &nextToken1); token2 = strtok_s(NULL, separator, &nextToken2); token3 = strtok_s(NULL, separator, &nextToken3); } Suppose '-' is my separator. The behaviour is

strtok_s behaviour with consecutive delimiters

微笑、不失礼 提交于 2019-12-30 11:03:13
问题 I'm parsing 3 values in parallel which are separated with a specific separator. token1 = strtok_s(str1, separator, &nextToken1); token2 = strtok_s(str2, separator, &nextToken2); token3 = strtok_s(str3, separator, &nextToken3); while ((token1 != NULL) && (token2 != NULL) && (token3 != NULL)) { //... token1 = strtok_s(NULL, separator, &nextToken1); token2 = strtok_s(NULL, separator, &nextToken2); token3 = strtok_s(NULL, separator, &nextToken3); } Suppose '-' is my separator. The behaviour is

strtok和strtok_r

跟風遠走 提交于 2019-12-30 08:19:26
strtok和strtok_r 原型:char *strtok(char *s, char *delim); 功能:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。 说明:首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。 strtok在s中查找包括在delim中的字符并用NULL('/0')来替换,直到找遍整个字符串。 返回值 :从s开头開始的一个个被切割的串。当没有被切割的串时则返回NULL。 全部delim中包括的字符都会被滤掉,并将被滤掉的地方设 为一处切割的节点。 举例: #include < string .h > #include < stdio.h > int main( void ) { char input[ 16 ] = " abc,d " ; char * p; /* strtok places a NULL terminator in front of the token, if found */ p = strtok(input, " , " ); if (p) printf( " %s " , p); /* A second call to strtok using a NULL as the first parameter returns a pointer to the character following

strtok和strtok_r

江枫思渺然 提交于 2019-12-30 08:19:03
strtok和strtok_r 原型:char *strtok(char *s, char *delim); 功能:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。 说明:首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。 strtok在s中查找包括在delim中的字符并用NULL('/0')来替换,直到找遍整个字符串。 返回值 :从s开头開始的一个个被切割的串。当没有被切割的串时则返回NULL。 全部delim中包括的字符都会被滤掉,并将被滤掉的地方设 为一处切割的节点。 举例: #include < string .h > #include < stdio.h > int main( void ) { char input[ 16 ] = " abc,d " ; char * p; /* strtok places a NULL terminator in front of the token, if found */ p = strtok(input, " , " ); if (p) printf( " %s " , p); /* A second call to strtok using a NULL as the first parameter returns a pointer to the character following

strtok和strtok_r

青春壹個敷衍的年華 提交于 2019-12-30 08:18:39
strtok和strtok_r 原型:char *strtok(char *s, char *delim); 功能:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。 说明:首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。 strtok在s中查找包括在delim中的字符并用NULL('/0')来替换,直到找遍整个字符串。 返回值 :从s开头開始的一个个被切割的串。当没有被切割的串时则返回NULL。 全部delim中包括的字符都会被滤掉,并将被滤掉的地方设 为一处切割的节点。 举例: #include < string .h > #include < stdio.h > int main( void ) { char input[ 16 ] = " abc,d " ; char * p; /* strtok places a NULL terminator in front of the token, if found */ p = strtok(input, " , " ); if (p) printf( " %s " , p); /* A second call to strtok using a NULL as the first parameter returns a pointer to the character following

strtok和strtok_r

血红的双手。 提交于 2019-12-30 08:18:24
原文:http://blog.csdn.net/hustfoxy/article/details/23473805 也可参考:http://www.cnblogs.com/lixiaohui-ambition/archive/2012/07/18/2598042.html 1、strtok函数 函数原型:char * strtok (char *str, const char * delimiters); 参数:str,待分割的字符串(c-string);delimiters,分割符字符串。 该函数用来将字符串分割成一个个片段。参数str指向欲分割的字符串,参数delimiters则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delimiters中包涵的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。 需要注意的是,使用该函数进行字符串分割时,会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。第一次分割之后,原字符串str是分割完成之后的第一个字符串,剩余的字符串存储在一个静态变量中,因此多线程同时访问该静态变量时,则会出现错误。 strtok例子: [cpp] view plain copy #include

strtok()和strtok_r()

一曲冷凌霜 提交于 2019-12-30 08:18:13
下面的说明摘自于最新的Linux内核2.6.29,说明了strtok()这个函数已经不再使用,由速度更快的strsep()代替 /* * linux/lib/string.c * * Copyright (C) 1991, 1992 Linus Torvalds */ /* * stupid library routines.. The optimized versions should generally be found * as inline code in <asm-xx/string.h> * * These are buggy as well.. * * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de> * - Added strsep() which will replace strtok() soon (because strsep() is * reentrant and should be faster). Use only strsep() in new code, please. * * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>, * Matthew Hawkins <matt@mh.dropbear.id.au> * -