4 ms·
Don't use strtok, it was already painful to use in C99. It is not even that hard to emulate using strstr if you do need one.
by lifthrasiir 2y ago
Don't use strtok, it was already painful to use in C99. It is not even that hard to emulate using strstr if you do need one.
- nesarkvechnep 2y agoThe OC is the type of person to tell you the problems of C are all skill issues.
- Lanolderen 2y agoAlways have been (⌐■_■)
- ape4 2y agoUse strtok_r()
- lifthrasiir 2y agoThat also works, but strtok is of so limited use that it's probably better to roll your own anyway. For example you can't handle arbitrary whitespace with strtok.
- deleted 2y ago[deleted]
- johnisgood 2y agoman -a strtok with all manual pages installed[1] (POSIX, Linux, BSD), you get a pretty good idea as to how to use it properly, or reading the SEI C Coding Standard helps. OpenBSD deprecated it in favor of strsep. It is not painful to use if you know (read all the manual pages at the very least). Been happily using it for a long time (unless other functions are not more ideal for the specific use-case). There are some things to pay attention to, but the manual pages pretty much tell you everything you need to know. [1] https://archlinux.org/packages/core/any/man-pages/ https://archlinux.org/packages/core/any/man-pages/ on Arch Linux, perhaps it is "man-pages" for other distros, too. As per Arch wiki: "man-pages provides both the Linux and the POSIX.1 man pages". You may also do: curl -sL 'https://man.archlinux.org/man/strtok.raw' | man -l - or curl -sL 'https://man.archlinux.org/man/getaddrinfo_a.raw' | man -l - for "strtok", although not sure it provides ALL manual pages.
- GuB-42 2y agoIf your use case is covered by strtok(), why not? Like all functions, you have to use it properly, and the use case for strtok() is narrow, but sometimes, it is just what you need. I rarely use it myself, but emulating it is silly. Also strstr() is probably not the most appropriate, maybe you mean strpbrk()? As far as I know, the only function from the libc that you should never use is gets(). It is even written in the manual: "Never use this function". As expected, it is removed from recent standards.
- johnisgood 2y agoExactly. There is also "strsep"[1]. [1] https://man.openbsd.org/strsep.3 https://man.openbsd.org/strsep.3
- kstrauser 2y agoDoesn't that modify the input in exactly the same way strtok does?
- duskwuff 2y agoThe worst problem with strtok() isn't that it modifies its input; it's that it's stateful. It retains a pointer to the input string internally, and returns subsequent words from it when called with an input of NULL. strsep() fixes this by using a char * argument instead.
- duskwuff 2y ago... and HN's formatting mangled that, yay. I meant a char ** argument.