strtok

Split string by a substring

强颜欢笑 提交于 2019-12-01 21:46:36
问题 I have following string: char str[] = "A/USING=B)"; I want to split to get separate A and B values with /USING= as a delimiter How can I do it? I known strtok() but it just split by one character as delimiter. 回答1: I known strtok() but it just split by one character as delimiter Nopes, it's not. As per the man page for strtok)() , ( emphasis mine ) char *strtok(char *str, const char *delim); [...] The delim argument specifies a set of bytes that delimit the tokens in the parsed string. [...]

implicit declaration of function ‘strtok_r’ [-Wimplicit-function-declaration] inspite including <string.h>

穿精又带淫゛_ 提交于 2019-12-01 17:59:40
I have the following code to tokenize a string containing lines separated by \n and each line has integers separated by a \t : void string_to_int_array(char file_contents[BUFFER_SIZE << 5], int array[200][51]) { char *saveptr1, *saveptr2; char *str1, *str2; char delimiter1[2] = "\n"; char delimiter2[2] = " "; char line[200]; char integer[200]; int j; for(j = 1, str1 = file_contents; ; j++, str1 = NULL) { line = strtok_r(str1, delimiter1, &saveptr1); if (line == NULL) { break; } for (str2 = line; ; str2 = NULL) { integer = strtok_r(str2, delimiter2, &saveptr2); if (integer == NULL) { break; } }

Unexpected strtok() behaviour

北慕城南 提交于 2019-12-01 17:44:52
I'm trying to count the number of words in a file with strtok(). /* * code.c * * WHAT * Use strtok() to count the number of words in a file. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define STRMAX 128 int main() { /* Declarations */ FILE* fptr; int iCntr = 0; char sLine[STRMAX]; char* cPToken; /* Read file */ /* Error handler */ if ((fptr = fopen("/home/ubuntu/Dropbox/Unief/C/H18/Opdr01/Debug/test.txt", "r")) == NULL) { printf("Couldn't read test.txt.\n"); exit(0); } else { while (fgets(sLine, STRMAX-1, fptr) != NULL) { /* Read line */ while ((cPToken = strtok(sLine, ".,;

Obtaining zero-length string from strtok()

时光毁灭记忆、已成空白 提交于 2019-12-01 17:33:02
问题 I have a CSV file containing data such as value;name;test;etc which I'm trying to split by using strtok(string, ";") . However, this file can contain zero-length data, like this: value;;test;etc which strtok() skips. Is there a way I can avoid strtok from skipping zero-length data like this? 回答1: A possible alternative is to use the BSD function strsep() instead of strtok() , if available. From the man page: The strsep() function is intended as a replacement for the strtok() function. While

Unexpected strtok() behaviour

大憨熊 提交于 2019-12-01 17:16:54
问题 I'm trying to count the number of words in a file with strtok(). /* * code.c * * WHAT * Use strtok() to count the number of words in a file. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define STRMAX 128 int main() { /* Declarations */ FILE* fptr; int iCntr = 0; char sLine[STRMAX]; char* cPToken; /* Read file */ /* Error handler */ if ((fptr = fopen("/home/ubuntu/Dropbox/Unief/C/H18/Opdr01/Debug/test.txt", "r")) == NULL) { printf("Couldn't read test.txt.\n"); exit(0); } else

strtok() issue: If tokens are delimited by delimiters,why is last token between a delimiter and the null '\0'?

百般思念 提交于 2019-12-01 16:47:02
问题 In the following program, strtok() works as expected in the major part but I just can't comprehend the reason behind one finding. I have read about strtok() that: To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the

Implementing `strtok` whose delimiter has more than one character

你。 提交于 2019-12-01 12:22:36
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 in the second argument of strtok is considered as a delimiter. To get the expected output, I've written

Split string into Tokens in C, when there are 2 delimiters in a row

╄→гoц情女王★ 提交于 2019-12-01 11:38:43
I am using strtok() function to split a string into Tokens.The problem is when there are 2 delimiters in row. /* strtok example */ #include <stdio.h> #include <string.h> int main () { char str[] ="Test= 0.28,0.0,1,,1.9,2.2,1.0,,8,4,,,42,,"; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str,", "); while (pch != NULL) { printf ("Token = %s\n",pch); pch = strtok (NULL, ", "); } return 0; } And outputs: Splitting string "Test= 0.28,0.0,1,,1.9,2.2,1.0,,8,4,,,42,," into tokens: Token = Test= Token = 0.28 Token = 0.0 Token = 1 Token = 1.9 Token = 2.2 Token = 1.0

Reading and parsing lines from a file with fgets and strtok

扶醉桌前 提交于 2019-12-01 06:47:29
I'm having trouble with a fairly basic bit of code. I need to read each line from the file shown below, split it up into the 3 parts with strtok, and store each part into an array. The arrays for "goals" and "assists" are working perfectly, but for some reason the entire name array is filled with the last name read from the file. Input file: Redden 2 0 Berglund 5 2 Jackman 2 0 Stewart 4 0 Oshie 3 5 McDonald 2 4 Pietrangelo 2 7 Perron 2 6 Tarasenko 5 5 Relevant code: int main(int argc, char* argv){ FILE* inFile = fopen(argv[1],"r"); char ** nameArray; int * goalArray; int * assistArray; int

strtok() and empty fields

时光总嘲笑我的痴心妄想 提交于 2019-12-01 04:23:08
问题 I am serializing some C structure to string and than deserializing it with strtok() . But, unfortunately, strtok() don't detect empty fields (eg 1:2::4). Is there any alternative function? 回答1: On linux there's strsep. The strsep() function was introduced as a replacement for strtok(), since the latter cannot handle empty fields. However, strtok() conforms to C89/C99 and hence is more portable. 回答2: You can use strchr (for just one delimiter character) or strcspn (for a group of possible