C/C++ >> strtok() function problem
Posted by sz200 on 19:35:00 03-15-2002
Hello,

I am trying to use the strtok() function, but I seem to be running into problems for specific cases in which it fails to tokenize.

For example, I have a char * allow_parms which contains the string : "bob1 bob2 bob3"

When I tokenize it using " " as delimeter :

for(word = strtok(allow_parms, " "); word != NULL; word = strtok(NULL , " "))

I get bob1, bob2, and bob3 separated fine.

However, when I have allow_parms set to the string : "/tmp/test1/* /tmp/test2/*" and passed through the for loop above.... My results are /tmp/test1/* and then NULL. For some strange reason, it seems to die. I think its because it has problems with the '*' character.

Is this a bug in strtok() or am I doing something wrong? Thanks!

-Sz
Posted by seunosewa on 23:51:00 03-31-2002
It seems your code is correct.

(a)Perhaps the problem is due to the context from which the code is being called (for example, from the command line). On Linux, for example, the '*' is expanded to include all files in the in the specified directory having the pattern.

For example "/root/*" becomes "/root/test.c /root/test2.c" if those are the two files in the root directory.

(b) Can you post the context within which this code snippet was extracted? Perhaps there is a memory allocation error somewhere,

(c) Maybe the code that does the counting has a problem, too.

(d)There's one more thing to take into account: The strtok function apparently overwrites the delimiter in the input string with '\0' as part of normal operation.

Hope this helps, somewhat. Though I'm yet to test the code .

Perhaps sites like:
http://www.opengroup.org/onlinepubs/7908799/xsh/strtok.html will help further


[ This Message was edited by: seunosewa on 2002-04-01 00:06 ]