C splitting char array based on a delimiter, but it is failing with consecutive delimiters -


i trying split char array in c using strtok. have working @ moment, have realised when there 2 consecutive delimiters concept gets offset.

i parsing char array structure (i cannot post exact code because assignment, post similar code assignment specifics changed) based on thier index, e.g.

struct test_struct{       int index_1;      int index_2;      int index_3;      int index_4;      int index_5;  }test_struct; 

i use counter populate information, every time delimiter reached increment counter , assign data index, e.g:

char c_array[50] = "hello,this,is,an,example"  counter = 0;  token = strtok (c_array,",");  while (token != null) {     switch(counter){                 case 0:                 test_struct.index_1 = token;                 break;                 case 1:                 test_struct.index_2 = token;                 break;                //repeat step other indexes   }   counter++;   token = strtok (null, ","); 

}

i know case switch poor design choice in situation, aside can me find solution problem:

the problem is, when char array (c string basically) contains consecutive delimiters, token "skips" index, throwing out of line. take above example

if char array formatted properly, when case 5 hits, have representing 5th "spit string" above example, when counter == 5 test_struct.index_5 have value "example".

now, if given above code if c_array[50] = "hello,this,,an,example" problem after there missing data in array messes indexing, "skip" next index because ,, doesn't have "string" inbetween them instead of intended behaviour this:

test_struct.index_1 = "hello" test_struct.index_2 = "this" test_struct.index_3 = "an" test_struct.index_4 = "example" test_struct.index_5 = "example" 

so there way if there "" set token default value, e.g. "missing data" @ least can handle separately after have read in data correct indexes.

i hope understand mean.

cheers, chris.

working code

nb: code still modifies input string, recognizes empty tokens quite happily.

#include <stdio.h> #include <string.h>  static void split(char *string) {     enum { max_strings = 5 };     struct test_struct     {         char *index[max_strings];     } test_struct;      printf("splitting: [%s]\n", string);      int = 0;     char *bgn = string;     char *end;     while (i < max_strings && (end = strpbrk(bgn, ",")) != 0)     {         test_struct.index[i++] = bgn;         *end = '\0';         bgn = end + 1;     }     if (i >= max_strings)         fprintf(stderr, "too many strings!\n");     else         test_struct.index[i++] = bgn;      (int j = 0; j < i; j++)         printf("index[%d] = [%s]\n", j, test_struct.index[j]); }  int main(void) {     char c_array[][30] =     {         "hello,this,is,an,example",         "hello,this,,an,example",         "hello,,bad,,example,input",         "hello,world",         ",,,,",         ",,",         "",     };     enum { c_size = sizeof(c_array) / sizeof(c_array[0]) };     (int = 0; < c_size; i++)         split(c_array[i]);     return 0; } 

example output

splitting: [hello,this,is,an,example] index[0] = [hello] index[1] = [this] index[2] = [is] index[3] = [an] index[4] = [example] splitting: [hello,this,,an,example] index[0] = [hello] index[1] = [this] index[2] = [] index[3] = [an] index[4] = [example] splitting: [hello,,bad,,example,input] many strings! index[0] = [hello] index[1] = [] index[2] = [bad] index[3] = [] index[4] = [example] splitting: [hello,world] index[0] = [hello] index[1] = [world] splitting: [,,,,] index[0] = [] index[1] = [] index[2] = [] index[3] = [] index[4] = [] splitting: [,,] index[0] = [] index[1] = [] index[2] = [] splitting: [] index[0] = [] 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -