C strtok question

Uli Kusterer kusterer at gmail.com
Wed Apr 11 02:27:51 PDT 2007


Am 11.04.2007 um 08:35 schrieb Koryn Grant:
> Not on the man page I have installed (stock standard Apple  
> Developer Tools for OS X 10.4).
>
> However, I would recommend using strtok_r() instead of strtok().   
> Bugs involving nested calls to strtok() are not pleasant to track  
> down.

  Well, I have the latest version installed (no modifications  
otherwise), and the first thing it says, in bold, under "DESCRIPTION"  
is "This interface is obsoleted by strsep(3)." Which is obvious when  
you read this part of strsep's manpage:

      The strsep() function is intended as a replacement for the  
strtok() func-
      tion.  While the strtok() function should be preferred for  
portability
      reasons (it conforms to ISO/IEC 9899:1990 (``ISO C90'')) it is  
unable to
      handle empty fields, i.e., detect fields delimited by two  
adjacent delim-
      iter characters, or to be used for more than a single string at  
a time.
      The strsep() function first appeared in 4.4BSD.

But you could do worse than using strtok_r(), obviously. By the way:  
If you're using Cocoa, you may want to look into NSScanner and  
NSString's componentsSeparatedByString:.

Cheers,
-- M. Uli Kusterer
http://www.zathras.de





More information about the MacOSX-dev mailing list