strtok

String parsing in C using strtok

半腔热情 提交于 2019-12-23 14:54:10
问题 I've got this little source code, made for testing the parsing of a string similar to variable string I need to use in other project #include <stdio.h> #include <stdlib.h> #include <string.h> int main (void) { char string[] = "C-AC-2C-3C-BOB"; char* s; char* hand[3]; char* usr; s = (char*) calloc(1, sizeof(char)); hand[1] = (char*) calloc(3, sizeof(char)); hand[2] = (char*) calloc(3, sizeof(char)); hand[3] = (char*) calloc(3, sizeof(char)); usr = (char*) calloc(21, sizeof(char)); s = strtok

boost库中thread多线程中的thread_specific_ptr

最后都变了- 提交于 2019-12-23 05:31:35
大多数函数都不是可重入的。这也就是说在某一个线程已经调用了一个函数时,如果你再调用同一个函数,那么这样是不安全的。举例来说,std::strtok就是不可重入的,因为它使用静态变量来保存要被分割成符号的字符串。 一个不可重入的函数通过连续的调用来保存静态变量或者是返回一个指向静态数据的指针,有两种方法可以让不可重用的函数变成可重用的函数。 方法1:就是改变接口,用指针或引用代替原先使用静态数据的地方。比方说,POSIX定义了strok_r,std::strtok中的一个可重入的变量,它用一个额外的char**参数来代替静态数据。这种方法很简单,而且提供了可能的最佳效果。但是这样必须改变公共接口,也就意味着必须改代码。 方法2:不用改变公有接口,而是用本地存储线程(Thread-Locally Storage)来代替静态数据(有时也被成为特殊线程存储,thread-specific storage)。 Boost线程库提供了智能指针boost::thread_specific_ptr来访问本地存储线程。thread_specific_ptr线程局部存储的包装,它可用于封装线程独立的全局变量。每一个线程第一次使用这个智能指针的实例时,它的初值是NULL(所以必须要先检查这个它的值是否为空),在每个线程使用它之前需要new一个对象交给全局的threah_specific_ptr进行管理

Error trying to use 'strtok' for a string

隐身守侯 提交于 2019-12-22 12:57:22
问题 #include <iostream> #include <string.h> using namespace std; int main() { char *tok; string s = "Ana and Maria are dancing."; tok = strtok(s.c_str(), " "); while(tok != NULL) { cout << tok << " "; tok = strtok(NULL, " "); } return 0; } And I'm getting this error: :9:29: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive] In file included from ceva.cc:2:0: 348:14: error: initializing argument 1 of ‘char* strtok(char*, const char*)’ [-fpermissive]" 回答1: strtok() is

How to group substrings in Teradata 14?

巧了我就是萌 提交于 2019-12-22 09:26:45
问题 I have the following table in Teradata 14 , I am not allowed to write procedures and functions myself, but i can use strtok , strtok_split_to_table etc id property 1 1234X (Yel), 2225Y (Red), 1234X (Gre), 2 3 1222Y (Pin), 4 1134E (Yel), 4565Y (Whi), 1134E (Red), 2222Y (Red), How can I group the above table so that each object would have all attributes listed in one brackets id property 1 1234X (Yel Gre), 2225Y (Red), 2 3 1222Y (Pin ), 4 1134E (Yel Red), 4565Y (Whi), 2222Y (Red), The property

strtok and memory leaks

我怕爱的太早我们不能终老 提交于 2019-12-21 05:11:07
问题 I wrote a simple url parser using strtok(). here's the code #include <stdio.h> #include <stdlib.h> typedef struct { char *protocol; char *host; int port; char *path; } aUrl; void parse_url(char *url, aUrl *ret) { printf("Parsing %s\n", url); char *tmp = (char *)_strdup(url); //char *protocol, *host, *port, *path; int len = 0; // protocol agora eh por exemplo http: ou https: ret->protocol = (char *) strtok(tmp, "/"); len = strlen(ret->protocol) + 2; ret->host = (char *) strtok(NULL, "/"); len

How to use Strtok for tokenizing a Const char*?

淺唱寂寞╮ 提交于 2019-12-20 18:16:32
问题 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. 回答1: Since strtok actually writes to your string

How to use Strtok for tokenizing a Const char*?

我怕爱的太早我们不能终老 提交于 2019-12-20 18:15:55
问题 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. 回答1: Since strtok actually writes to your string

Breaking down string and storing it in array

和自甴很熟 提交于 2019-12-20 10:44:36
问题 I want to break down a sentence and store each string in an array. Here is my code: #include <stdio.h> #include <string.h> int main(void) { int i = 0; char* strArray[40]; char* writablestring= "The C Programming Language"; char *token = strtok(writablestring, " "); while(token != NULL) { strcpy(strArray[i], token); printf("[%s]\n", token); token = strtok(NULL, " "); i++; } return 0; } It keeps giving me segmentation error and I cannot figure it out. I believe it has something to do when I

strtok Unhandled exception;Access violation writing location

我是研究僧i 提交于 2019-12-20 05:52:18
问题 #include <stdio.h> #include <time.h> #include <string.h> char *matrix[10][10]; int main(void) { int i; char *list[4]; char *words[20] = { " c a t ", " c a r ", " b e a r ", " s h i p ", " m o u s e ", " b e a t l e ", " c o a t ", " n e s t ", " i c e ", " s u g a r ", " b a c o n ", " f r o w n ", " s m i l e ", " d e a d ", " f e a t h e r ", " g o a t ", " h e n "," j e l l y "," k o a l a "," l i p s " }; int length; int num; int k; int m; char otherString=0; char *c; int j; int s; int r;

Empty check with string split

此生再无相见时 提交于 2019-12-20 05:38:42
问题 vector<string> SplitString (string aString,char *sep) { vector<string> vec; char * cstr,*val,*p; string str = aString; cstr = new char [str.size()+1]; strcpy (cstr, str.c_str()); p=strtok (cstr,sep); while(p!=NULL) { vec.push_back(p); p=strtok(NULL,sep); }delete[] cstr;return vec; } This is my code to string split. I sent the below string to split with separator '&' "f0=fname0&l0=lname0&f1=fname1&l1=lname1&f2=fname2&l2=lname2&f3=&l3=". I got result in the vector as below. f0=fname0 l0=lname0