strtok

c++中字符串分割(split)

核能气质少年 提交于 2019-11-27 13:35:31
函数第一次调用需设置两个参数。第一次分割的结果,返回串中第一个" " 之前的字符串,也就是上面的程序第一次输出abc。 第二次调用该函数strtok(NULL," "),第一个参数设置为NULL。结果返回分割依据后面的字串,即第二次输出d。 strtok是一个线程不安全的函数,因为它使用了静态分配的空间来存储被分割的字符串位置 线程安全的函数叫strtok_r, 运用strtok来判断ip或者mac的时候务必要先用其他的方法判断’.‘或’:'的个数,因为用strtok截断的话,比如:"192…168.0…8…"这个字符串,strtok只会截取四次,中间的…无论多少都会被当作一个key #include<iostream> #include<vector> #include<stdio.h> #include<string.h> using namespace std; int main(){ char sentence[1024]; scanf("%[^.]",&sentence);// .作为字符串输入的结束符 char *tokenPtr=strtok(sentence," ");//sentence必须是一个char数组,不能是定义成指针形式 vector<string> v; while(tokenPtr!=NULL){ v.push_back(tokenPtr);

Why is strtok changing its input like this?

笑着哭i 提交于 2019-11-27 13:16:17
Ok, so I understand that strtok modifies its input argument, but in this case, it's collapsing down the input string into only the first token. Why is this happening, and what can I do to fix it? (Please note, I'm not talking about the variable "temp", which should be the first token, but rather the variable "input", which after one call to strtok becomes "this") #include <string.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char* argv[]) { char input[]="this is a test of the tokenizor seven"; char * temp; temp=strtok(input," "); printf("input: %s\n", input); //input is now just

Segmentation Fault when using strtok_r

六眼飞鱼酱① 提交于 2019-11-27 12:22:59
问题 Can anyone explain why I am getting segmentation fault in the following example? #include <stdio.h> #include <string.h> int main(void) { char *hello = "Hello World, Let me live."; char *tokens[50]; strtok_r(hello, " ,", tokens); int i = 0; while(i < 5) { printf("%s\n", tokens[i++]); } } 回答1: Try this: #include <stdio.h> #include <string.h> int main(void) { char hello[] = "Hello World, Let me live."; // make this a char array not a pointer to literal. char *rest; // to point to the rest of the

strtok causing segfault but not when step through code

别等时光非礼了梦想. 提交于 2019-11-27 09:54:19
I am new to C and I am trying to split a date/time string into separate variables. However, when I step through the code in gdb line by line, it works, however, when I let it run through normally without breakpoints it seg faults and I can't see why. Below is the code: char * dateTimeString = "2011/04/16 00:00"; char dateVar[11]; char timeVar[6]; if (splitTimeAndDateString(dateVar, timeVar, dateTimeString)) { exit(1); } printf("Date: %s\tTime: %s\n", dateVar, timeVar); Below is the function int splitTimeAndDateString(char date[11], char time[6], char * dateString) { char *token; token = strtok

strtok_r for MinGW

谁都会走 提交于 2019-11-27 07:38:17
问题 strtok_r is the reentrant variant of strtok. It is POSIX-conformant. However, it is missing from MinGW, and I'm trying to compile a program that is using it. Is there any way I could add a standard implementation of this function, perhaps to the project's own code, or to MinGW's standard library functions? 回答1: Since there are some license questions about the code from another answer, here's one that's explicitly public domain: /* * public domain strtok_r() by Charlie Gordon * * from comp

strtok wont accept: char *str

倖福魔咒の 提交于 2019-11-27 06:34:24
问题 strtok wont work correctly when using char *str as the first parameter (not the delimiters string). Does it have something to do with the area that allocates strings in that notation? (which as far as i know, is a read-only area). thanks in advance example: //char* str ="- This, a sample string."; // <---doesn't work char str[] ="- This, a sample string."; // <---works char delims[] = " "; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str,delims); while (pch

How does the strtok function in C work? [duplicate]

ぐ巨炮叔叔 提交于 2019-11-27 05:33:46
问题 This question already has answers here : How does strtok() split the string into tokens in C? (13 answers) Closed 5 years ago . I found this sample program which explains the strtok function: #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; } However, I don't see how this is

Split string into tokens and save them in an array

大憨熊 提交于 2019-11-27 05:19:50
问题 How to split a string into an tokens and then save them in an array? Specifically, I have a string "abc/qwe/jkh" . I want to separate "/" , and then save the tokens into an array. Output will be such that array[0] = "abc" array[1] = "qwe" array[2] = "jkh" please help me 回答1: #include <stdio.h> #include <string.h> int main () { char buf[] ="abc/qwe/ccd"; int i = 0; char *p = strtok (buf, "/"); char *array[3]; while (p != NULL) { array[i++] = p; p = strtok (NULL, "/"); } for (i = 0; i < 3; ++i)

Is strtok broken? Or just tricky?

本秂侑毒 提交于 2019-11-27 04:49:06
问题 Is strtok hopelessly broken? On many StackOverflow questions about text-parsing in C, someone will suggest using strtok , and one common reply is that strtok should never be used, that it is hopelessly broken. Some posters have claimed that strtok 's problems are limited to multi-threading issues, and it is safe in a single-threaded environment. What is the right answer? Does it work? Is it hopelessly broken? Can you back up your answer with examples? 回答1: Yes, strtok is hopelessly broken,

C: creating array of strings from delimited source string

让人想犯罪 __ 提交于 2019-11-27 03:41:02
问题 What would be an efficient way of converting a delimited string into an array of strings in C (not C++)? For example, I might have: char *input = "valgrind --leak-check=yes --track-origins=yes ./a.out" The source string will always have only a single space as the delimiter. And I would like a malloc'ed array of malloc'ed strings char *myarray[] such that: myarray[0]=="valgrind" myarray[1]=="--leak-check=yes" ... Edit I have to assume that there are an arbitrary number of tokens in the