Re: looping through CSV list
Posted in 2011
On Wed, Jan 26, 2011 at 09:32, Ian Michael Gumby <im_gumby@hotmail.com>wrote: > In C, why not use strtok and create tokens using the delimiter of ',' ? > Then you do an string to int conversion and then comparison. > > Much easier. > > And no, I'm not going to print out the code. That's left as an exercise of > the user. (Hint: Google C and strtok example ) > Disquisition on the iniquities of strtok() - you can ignore this if you like (but at your own peril) Be wary of strtok(). If your code doesn't call any other functions that use strtok(), and if none of the functions that ever call your code is in the middle of using strtok(), then using strtok() is OK. If you cannot make both sets of guarantees (and library functions cannot), using strtok() yourself makes a mess of other places using it - it would be crucial for a library function to document that it uses strtok(), for example. You can often be reasonably confident about the functions you are calling; it is much harder to be confident about the functions that call yours. Clearly, if the strtok() is used in your main() program, you're likely to be OK - if your called functions are clean. Use strtok_r() where available. Use another algorithm altogether where you can't use strtok_r(). Note that strtok() and strtok_r() both mangle their input strings, placing nulls at the ends of tokens. That means you can't operate on string literals, etc. -- Jonathan Leffler <jonathan.leffler@gmail.com> #include <disclaimer.h> Guardian of DBD::Informix - v2008.0513 - http://dbi.perl.org "Blessed are we who can laugh at ourselves, for we shall never cease to be amused."