I am trying to tokenize a string but I need to know exactly when no data is seen between two tokens. e.g when tokenizing the following string “a,b,c,,,d,e” I need to know about the two empty slots between ‘d‘ and ‘e‘… which I am unable to find out simply using strtok(). My attempt is shown below:
char arr_fields[num_of_fields];
char delim[]=",\n";
char *tok;
tok=strtok(line,delim);//line contains the data
for(i=0;i<num_of_fields;i++,tok=strtok(NULL,delim))
{
if(tok)
sprintf(arr_fields[i], "%s", tok);
else
sprintf(arr_fields[i], "%s", "-");
}
Executing the above code with the aforementioned examples put characters a,b,c,d,e into first five elements of arr_fields which is not desirable. I need the position of each character to go in specific indexes of array: i.e if there is a character missing between two characters, it should be recorded as is.
7.21.5.8 the strtok function
The standard says the following regarding
strtok:In the above quote we can read you cannot use
strtokas a solution to your specific problem, since it will treat any sequential characters found indelimsas a single token.Am I doomed to weep in silence, or can somebody help me out?
You can easily implement your own version of
strtokthat does what you want, see the snippets at the end of this post.strtok_singlemakes use ofstrpbrk (char const* src, const char* delims)which will return a pointer to the first occurrence of any character in delims that is found in the null-terminated string src.If no matching character is found the function will return NULL.
strtok_single
sample use
output