views:

663

answers:

3

I am trying to use strtok() in nested loop. But this is not giving me desired results. Possibly because they are using same memory location. My code is of the form:-

char *token1 = strtok(Str1, "%");
while(token1 != NULL )
{
  char *token2 = strtok(Str2, "%");
  while(token2 != NULL )
   {
      //DO SMTHING
      token2 = strtok(NULL, "%");
    }
     token1 = strtok(NULL, "%");
     // Do something more
 }
+6  A: 

Yes, strtok(), indeed, uses some static memory to save its context between invocations. Use a reentrant version of strtok(), strtok_r() instead, or strtok_s() if you are using VS (identical to strtok_r()).

It has an additional context argument, and you can use different contexts in different loops.

char *tok, *saved;
for (tok = strtok_r(str, "%", &saved); tok; tok = strtok_r(NULL, "%", &saved))
{
    /* Do something with "tok" */
}
Alex B
In case you don't edit in the reason why `strtok` is behaving this way, here's some more information about `strtok_r`: http://www.mkssoftware.com/docs/man3/strtok_r.3.asp
Welbog
@ChrisF: The issue isn't the delimiter or where he's storing the tokens, but rather the fact that `strtok` can only token one string at any given time. `strtok_r` solves this problem by using another parameter to record its state.
Welbog
A: 

strtok is using a static buffer. In your case you should use strtok_r. This function is using a buffer provided by the user.

Patrice Bernassola
A: 

WayneAKing posted an alternative in the Microsoft Developer Center.

Citing him:

Go here

http://cpp.snippets.org/code/

and download this file

Stptok.C Improved tokenizing function

You can also download the needed header files from the same site.

This is a modified version of strtok which places the parsed tokens (substrings) in a separate buffer. You should be able to modify it to accommodate your needs.

  • Wayne

P.S. - Note that these files may be in *nix format with respect to end-of-lines. i.e. - 0x0A only and not 0x0D 0x0A

This is an alternative if you don't have the Microsoft libraries in your environment.

Hope it helps others : )

SDReyes