strtok

Extract string between two specific strings in C

只谈情不闲聊 提交于 2019-12-02 10:34:59
问题 How do you extract strings between two specified strings? For Example: <title>Extract this</title> . Is there a simple way to get it using strtok() or anything simpler? EDIT: The two specified strings are <title> and </title> and the string extracted is Extract this . 回答1: Search for the first sub string using strstr() . If found, save the array index of the sub string From there, search for the next sub string. If found, everything between [ [start of sub string 1] + [length of sub string 1]

strtok problem in calling

不羁的心 提交于 2019-12-02 10:22:23
问题 I have a function using strtok like this void f1(char *name) { ... char *tmp; tmp = strtok(names, " ,"); while(tmp) { ... tmp = strtok(NULL, " ,"); } ... } And i have a call f1("abc,def"); Problem is that in first call f1 gets abc,def and in 2nd call gets just abc I am confused.. Why is this so? 回答1: strtok() modifies its input string by overwriting the delimiters with a 0; so, assuming your code looks something like this: char parm[] = "abc,def"; f1(parm); f1(parm); after the first call to

reading multiple variable types from single line in file C

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 09:00:07
Alright I've been at this all day and can't for the life of me get this down, maybe you chaps can help. I have a file that reads as follows 1301,105515018,"Boatswain","Michael R.",ABC, 123,="R01" 1301,103993269,"Castille","Michael Jr",ABC, 123,="R03" 1301,103993267,"Castille","Janice",ABC, 123,="R03" 1301,104727546,"Bonczek","Claude",ABC, 123,="R01" 1301,104731479,"Cruz","Akeem Mike",ABC, 123,="R01" 1301,105415888,"Digiacomo","Stephen",ABC, 123,="R02" 1301,106034479,"Annitto Grassis","Susan",ABC, 123,="R04" 1301,106034459,"Als","Christian",ABC, 123,="R01" And here is my code... #include <stdio

Empty check with string split

感情迁移 提交于 2019-12-02 08:29:13
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 f1=fname1 l1=lname1 f2=fname2 l2=lname2 f3= l3= Now I again the sent the resulted strings with

C语言进行csv文件数据的读取

為{幸葍}努か 提交于 2019-12-02 07:57:40
C语言进行csv文件数据的读取: #include <stdio.h> #include <string.h> #include <malloc.h> #include <stdlib.h> #include <math.h> int main(){ FILE *fp = NULL; char *line,*record; char buffer[20450];//20450这个数组大小也要根据自己文件的列数进行相应修改。 if((fp = fopen("All-w.csv", "r")) != NULL) { fseek(fp, 16415L, SEEK_SET); //定位到第二行,每个英文字符大小为1,16425L这个参数根据自己文件的列数进行相应修改。 while ((line = fgets(buffer, sizeof(buffer), fp))!=NULL)//当没有读取到文件末尾时循环继续 { record = strtok(line, ","); while (record != NULL)//读取每一行的数据 { printf("%s ", record);//将读取到的每一个数据打印出来 record = strtok(NULL, ","); } } fclose(fp); fp = NULL; } } 来源: https://www.cnblogs.com

strtok behavior

99封情书 提交于 2019-12-02 07:27:43
问题 int main () { char str[] ="kk,12,,23,4,,,3434,3,33,,,"; char * valarr; int count=0; valarr = strtok(str,","); while(valarr != '\0') { valarr = strtok(NULL,","); count++; } printf("%d\n",count); return 0; } In above program the output is 7. It seems that the strtok is tokenizing consecutive commas at once. Instead of consecutive commas I can introduce a blank in between but Is there a way to overcome this so that I have empty space also in the count ? 回答1: Correct. The documentation states

strtok problem in calling

房东的猫 提交于 2019-12-02 05:19:10
I have a function using strtok like this void f1(char *name) { ... char *tmp; tmp = strtok(names, " ,"); while(tmp) { ... tmp = strtok(NULL, " ,"); } ... } And i have a call f1("abc,def"); Problem is that in first call f1 gets abc,def and in 2nd call gets just abc I am confused.. Why is this so? strtok() modifies its input string by overwriting the delimiters with a 0; so, assuming your code looks something like this: char parm[] = "abc,def"; f1(parm); f1(parm); after the first call to f1, the ',' character is overwritten with a 0, which is a string terminator, so the second call only sees

Extract string between two specific strings in C

别等时光非礼了梦想. 提交于 2019-12-02 05:02:17
How do you extract strings between two specified strings? For Example: <title>Extract this</title> . Is there a simple way to get it using strtok() or anything simpler? EDIT: The two specified strings are <title> and </title> and the string extracted is Extract this . Lundin Search for the first sub string using strstr() . If found, save the array index of the sub string From there, search for the next sub string. If found, everything between [ [start of sub string 1] + [length of sub string 1] ] and [start of sub string 2] is the string you are interested in. Extract the string using strncpy(

strtok when process two strings at same time

旧街凉风 提交于 2019-12-02 03:22:14
New in C and pretty confused about how to deal with several strings at the same time using strtok, for a simply example, I want to use strtok to extract the number and compare then. #include <stdio.h> #include <string.h> int main() { char s1[100]="11.54"; char s2[100]="12.55"; const char tok[2]="."; char* token1=strtok(s1,tok); char* token2=strtok(s2,tok); while(token1 !=NULL && token2 !=NULL){ int temp=strcmp(token1,token2); if(temp==0){ token1=strtok(NULL,tok); token2=strtok(NULL,tok); } else if(temp<0){ printf("%d\n",-1); return; } else{ printf("%d\n",1); return; } } if(token1 !=NULL){

strtok with space delimiter [closed]

喜你入骨 提交于 2019-12-02 01:39:23
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 Michael Burr 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; } Use tab "\t" or use both " \t" ie. space and