|
I wouldn't say to *never* use strtok, but you had better know what it does before using it. The thing about strtok is that it will modify the string you pass it, and that can be very dangerous if you don't know it's going to do that. Basically, the way strtok works is to look for your separator character, in this case a space " ", and when it finds one it replaces the character with a null character '\0', and then returns a pointer to the beginning of the text it found. Because C strings are null terminated when you do anything with this string, such as strcpy (string copy) it will copy until it hits the null terminator. On subsequent calls to strtok it will change the pointer to the character right after the null and look for the next deliminator " " and replace it with null, etc... This is actually a fairly good way to do this, but the manual is right, it will not work for constant strings, since they can't be changed. This function modifies the argument (the passed in string). The identity of the deliminating character is lost because it's replaced with NULL. As for it being thread safe, not much to say about that. But as long as you realize what it does and how it works, it would be fine to use. I do not know what strtok does when there are multiple deliminators though in the text string. That is, if I have the string "this is a test", I'm not sure if it comes back with "this" "is" "a" "test" or "this" is" "" "" "a" "test" or what. That you should probably experiment with and see what it does. Regards, Jim Langston -----Original Message----- From: James Rich [mailto:james@eaerich.com] On Fri, 19 Apr 2002, Nicolay, Paul wrote: > Funny to know that strtok() is a C function and isn't used in the provided C > code :-) Hmm... I didn't even know about the strtok() function. Oh here's why - from the man page: <SNIP> Even the man page for strtok() says not to use it. Now I know why I never heard of it. But I can't see any difference between strtok() and my little getword() function, other than strtok() uses a static buffer and getword() doesn't. That should fix the threadsafe problem.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.