strtok

C - Determining which delimiter used - strtok()

巧了我就是萌 提交于 2019-12-04 19:47:38
问题 Let's say I'm using strtok() like this.. char *token = strtok(input, ";-/"); Is there a way to figure out which token actually gets used? For instance, if the inputs was something like: Hello there; How are you? / I'm good - End Can I figure out which delimiter was used for each token? I need to be able to output a specific message, depending on the delimiter that followed the token. 回答1: Important: strtok is not re-entrant, you should use strtok_r instead of it. You can do it by saving a

Why does the following C program give a bus error?

て烟熏妆下的殇ゞ 提交于 2019-12-04 05:42:27
I think it's the very first strtok call that's failing. It's been a while since I've written C and I'm at a loss. Thanks very much. #include <stdio.h> #include <string.h> int main(int argc, char **argv) { char *str = "one|two|three"; char *tok = strtok(str, "|"); while (tok != NULL) { printf("%s\n", tok); tok = strtok(NULL, "|"); } return 0; } String literals should be assigned to a const char*, as modifying them is undefined behaviour. I'm pretty sure that strtok modifies it's argument, which would explain the bad things that you see. There are 2 problems: Make str of type char[] . GCC gives

Implementing `strtok` whose delimiter has more than one character

拜拜、爱过 提交于 2019-12-04 02:11:26
问题 Code snippet: char str[] = "String1::String2:String3:String4::String5"; char *deli = "::"; char *token = strtok(str,deli); while(token != NULL) { printf("Token= \"%s\"\n", token); token=strtok(NULL,deli); } The above code snippet produces the output: Token="String1" Token="String2" Token="String3" Token="String4" Token="String5" but I want the output to be: Token="String1" Token="String2:String3:String4" Token="String5" I know that I am not getting the expected output because each character

Do I need to free the strtok resulting string?

蹲街弑〆低调 提交于 2019-12-04 01:24:35
Or rather, how does strtok produce the string to which it's return value points? Does it allocate memory dynamically? I am asking because I am not sure if I need to free the token in the following code: The STANDARD_INPUT variables is for exit procedure in case I run out of memory for allocation and the string is the tested subject. int ValidTotal(STANDARD_INPUT, char *str) { char *cutout = NULL, *temp, delim = '#'; int i = 0; //Checks the number of ladders in a string, 3 is the required number temp = (char*)calloc(strlen(str),sizeof(char)); if(NULL == temp) Pexit(STANDARD_C); //Exit function,

C - Determining which delimiter used - strtok()

放肆的年华 提交于 2019-12-03 12:03:53
Let's say I'm using strtok() like this.. char *token = strtok(input, ";-/"); Is there a way to figure out which token actually gets used? For instance, if the inputs was something like: Hello there; How are you? / I'm good - End Can I figure out which delimiter was used for each token? I need to be able to output a specific message, depending on the delimiter that followed the token. Important: strtok is not re-entrant, you should use strtok_r instead of it. You can do it by saving a copy of the original string, and looking into offsets of the current token into that copy: char str[] = "Hello

Need to know when no data appears between two token separators using strtok()

匿名 (未验证) 提交于 2019-12-03 09:02:45
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to tokenize a string but I need to know exactly when no data is seen between two tokens. e.g when tokenizing the following string " a,b,c,,,d,e " I need to know about the two empty slots between ' d ' and ' e '... which I am unable to find out simply using strtok() . My attempt is shown below: char arr_fields[num_of_fields]; char delim[]=",\n"; char *tok; tok=strtok(line,delim);//line contains the data for(i=0;i Executing the above code with the aforementioned examples put characters a,b,c,d,e into first five elements of arr

Why should strtok() be deprecated?

匿名 (未验证) 提交于 2019-12-03 08:35:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I hear this from a lot of programmers that the use of strtok maybe deprecated in near future. Some say it is still. Why is it a bad choice? strtok() works great in tokenizing a given string. Does it have to do anything with the time and space complexities? Best link I found on the internet was this . But that doesn't seem to solve my curiousity. Suggest any alternatives if possible. 回答1: Why is it a bad choice? The fundamental technique for solving problems by programming is to construct abstractions which can be used reliably to

How to use Strtok for tokenizing a Const char*?

◇◆丶佛笑我妖孽 提交于 2019-12-03 05:00:58
I have a const char* variable which may have a value like "OpenStack:OpenStack1". I want to tokenize this const char* using strtok where the delimiter(which is of a const char* type) is ":" . But the problem is strtok is of following type: char * strtok ( char * str, const char * delimiters ); Which means I can't use const char* for the first input as it has to be char*. Could you say me how I can convert this const char* into char*? Thank you. Since strtok actually writes to your string, you need to make a writable copy of it to tokenize; char* copy = strdup(myReadonlyString); ...tokenize

strtok - char array versus char pointer [duplicate]

為{幸葍}努か 提交于 2019-12-03 04:33:11
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: strtok wont accept: char *str When using the strtok function, using a char * instead of a char [] results in a segmentation fault. This runs properly: char string[] = "hello world"; char *result = strtok(string, " "); This causes a segmentation fault: char *string = "hello world"; char *result = strtok(string, " "); Can anyone explain what causes this difference in behaviour? 回答1: char string[] = "hello world";

strtok with space delimiter [closed]

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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.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; }