0

I have seen the following piece of code in one of the library. What is the behavior of strtok when empty string is passed as a delimiter? I can see whatever buf contains, stored into token variable after strtok call.

char buf[256] = {0};
char token = NULL;

...
...
while (!feof(filePtr))
{
  os_memset(buf, 0, sizeof(buf));
  if (!fgets(buf, 256, filePtr)) 
  {
    token = strtok(buf, "");
    ...
    ...
  }
}
Arun
  • 2,247
  • 3
  • 28
  • 51
  • 1
    [Please see Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/2173917) – Sourav Ghosh Jan 23 '18 at 09:28
  • 1
    I don't see the point of that specific `strtok` call. It's just the same as doing `token = buf` (if `token` was a *pointer* to `char`, and not a single `char`). Is that actual code you have found? Where did you find it? Perhaps it's bug? Or perhaps the author originally had some delimiters in the string, but removed them in a later version? It's really impossible for us to tell the reason, you have to ask the author. – Some programmer dude Jan 23 '18 at 09:33
  • @Barmar OK sir, now I get it, thanks, let me clean it up. – Sourav Ghosh Jan 23 '18 at 09:55

1 Answers1

1

strtok() starts by looking for the first character not in the delimiter list, to find the beginning of a token. Since all characters are not in the delimiter list, the first character of the string will be the beginning of the token.

Then it looks for the next character in the delimiter list, to find the end of the token. Since there are no delimiters, it will never find any of them, so it stops at the end of the string.

As a result, an empty delimiter list means the entire string will be parsed as a single token.

Why he wrote it like this is anyone's guess.

Barmar
  • 741,623
  • 53
  • 500
  • 612