strtok

C's strtok() and read only string literals

我的未来我决定 提交于 2019-11-26 10:02:34
问题 char *strtok(char *s1, const char *s2) repeated calls to this function break string s1 into \"tokens\"--that is the string is broken into substrings, each terminating with a \'\\0\', where the \'\\0\' replaces any characters contained in string s2. The first call uses the string to be tokenized as s1; subsequent calls use NULL as the first argument. A pointer to the beginning of the current token is returned; NULL is returned if there are no more tokens. Hi, I have been trying to use strtok

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

南笙酒味 提交于 2019-11-26 09:57:15
问题 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<num_of_fields;i++,tok=strtok(NULL,delim)) { if(tok) sprintf

How to split a string to 2 strings in C

时间秒杀一切 提交于 2019-11-26 09:19:59
问题 I was wondering how you could take 1 string, split it into 2 with a delimiter, such as space, and assign the 2 parts to 2 separate strings. I\'ve tried using strtok() but to no avail. 回答1: #include <string.h> char *token; char line[] = "SEVERAL WORDS"; char *search = " "; // Token will point to "SEVERAL". token = strtok(line, search); // Token will point to "WORDS". token = strtok(NULL, search); Update Note that on some operating systems, strtok man page mentions: This interface is obsoleted

strtok function thread safety

孤者浪人 提交于 2019-11-26 06:49:52
问题 I have been spending some time in debugging a programme which gives segmentation fault. The bug is quite indeterministic and intermittent, which is annoying. I narrowed it down to the calling of strtok() . I suspect that it is the calling of strtok() to split string in two different threads that causes the segmentation fault. Can I call strtok() in two different threads? Thanks. 回答1: strtok() is not reentrant so it should not be used from threaded applications, use strtok_r() instead. 回答2:

Why is strtok() Considered Unsafe?

五迷三道 提交于 2019-11-26 05:58:43
问题 What feature(s) of strtok is unsafe (in terms of buffer overflow) that I need to watch out for? What\'s a little weird to me is that strtok_s (which is \"safe\") in Visual C++ has an extra \"context\" parameter, but it looks like it\'s the same in other ways... is it the same, or is it actually different? 回答1: According with the strtok_s section of this document: 6.7.3.1 The strtok_s function The strtok_s function fixes two problems in the strtok function: A new parameter, s1max, prevents

Using strtok in c

风流意气都作罢 提交于 2019-11-26 04:28:32
问题 I need to use strtok to read in a first and last name and seperate it. How can I store the names where I can use them idependently in two seperate char arrays? #include <stdio.h> #include <string.h> int main () { char str[] =\"test string.\"; char * test; test = strtok (str,\" \"); while (test != NULL) { printf (\"%s\\n\",test); test= strtok (NULL, \" \"); } return 0; } 回答1: Here is my take at a reasonably simple tokenize helper that stores results in a dynamically growing array null

Nested strtok function problem in C [duplicate]

戏子无情 提交于 2019-11-26 03:59:46
问题 This question already has an answer here: Using strtok() in a loop in C? 3 answers I have a string like this: a;b;c;d;e f;g;h;i;j 1;2;3;4;5 and i want to parse it element by element. I used nested strtok function but it just splits first line and makes null the token pointer. How can i overcome this? Here is the code: token = strtok(str, \"\\n\"); while(token != NULL && *token != EOF) { char a[128], b[128]; strcpy(a,token); strcpy(b,a); printf(\"a:%s\\n\",a); char *token2 = strtok(a,\";\");

Using strtok() in a loop in C?

我们两清 提交于 2019-11-26 02:58:59
问题 I am trying to use strtok() in nested loop. But this is not giving me desired results. Possibly because they are using same memory location. My code is of the form:- char *token1 = strtok(Str1, \"%\"); while(token1 != NULL ) { char *token2 = strtok(Str2, \"%\"); while(token2 != NULL ) { //DO SMTHING token2 = strtok(NULL, \"%\"); } token1 = strtok(NULL, \"%\"); // Do something more } 回答1: Yes, strtok() , indeed, uses some static memory to save its context between invocations. Use a reentrant

How does strtok() split the string into tokens in C?

♀尐吖头ヾ 提交于 2019-11-25 23:48:37
问题 Please explain me the working of strtok() function.The manual says it breaks the string into tokens. I am unable to understand from the manual what actually it does. I added watches on str and *pch to check its working, when the first while loop occurred, the contents of str were only \"this\". How did the output shown below printed on the screen? /* strtok example */ #include <stdio.h> #include <string.h> int main () { char str[] =\"- This, a sample string.\"; char * pch; printf (\"Splitting