When I read data from a string with data that is separated with commas using strtok, the value that is being read is shifted if the value that was read before was (null).
int main(void)
{
char Temp[10] = "1,2,3,,4";
printf("%s\n", strtok(Temp, ","));
printf("%s\n", strtok(NULL, ","));
printf("%s\n", strtok(NULL, ","));
printf("%s\n", strtok(NULL, ","));
printf("%s\n", strtok(NULL, ","));
}
Expected Output:
1
2
3
(null)
4
Real Output:
1
2
3
4
(null)
The same thing happens even if there are 2 blanks. It just moves over twice, rather than once.
When
strtokparses a string, any consecutive delimiter characters will be grouped together. This basically means that you'll never see a blank token until you reach the end of the string.The man page states the following:
So what you're seeing is the expected behavior.
If you want to be able to treat consecutive delimiters as a blank token, you'll need use
strchrto find each delimiter and copy out the relevant substrings yourself.