In defense of scanf() (Re: Re^2: scanf(..))

Dale Schumacher dal at midgard.Midgard.MN.ORG
Fri Aug 4 05:22:05 AEST 1989


In article <4596 at ficc.uu.net> peter at ficc.uu.net (Peter da Silva) writes:
|In article <824 at cbnewsl.ATT.COM>, mpl at cbnewsl.ATT.COM (michael.p.lindner) writes:
|> Hope you never have to do anything complex.  If you call strtok on e string
|> in the middle of a strtok of another string it trashes its state information
|> on the first string (a little known feature), which can cause extremely
|> elusive bugs.
|
|Sounds like something too fancy for scanf, too. But thanks for the info... I've
|never gotten that complex with strtok. By that time I'm usually stepping
|through the string by hand (while (strchr(*s, legalchars)) s++;)...

Be careful here.  Since strchr() will match the '\0' at the end of legalchars,
you may walk right out of the string if all characters are legal!  I use a
macro like the following:

#define	IN_SET(set,c)	((c) && strchr((set), (c)))

Note, this is NOT a "safe" macro, so be sure c has no side-effects...

|[files away information that there are broken implementations of strtok...]

The operation of strtok() described above is NOT broken, it's documented.
It is also somewhat less useful than it could be due to it's "interesting"
quirks, but it IS defined as working that way.



More information about the Comp.lang.c mailing list