Live data from Hacker News

String tokenization in C

onebyezero.blogspot.com

1–10 of 117 posts

Re: String tokenization in C

#2
Well, yes, using strtok works if the data happens to be structured in a certain simple way. Very often you want to do something more advanced though, and using regex for matching tokens is then necessary.

Re: String tokenization in C

#5
post #4
post #3

It used to be that gcc will warn against strtok and recommend strsep instead. Do not know what the status is today

Strtok is not thread safe and can’t be made thread safe without changing the API. You should not use it.

> Strtok is not thread safe and can’t be made thread safe without changing the API. You should not use it.

Well, there is already a thread-safe variant [0]: > The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.

[0] https://linux.die.net/man/3/strtok_r

Re: String tokenization in C

#10
post #9
post #6

Note though that strsep() is not as portable, because it is an extension to standard C.

In fact, it's not even in POSIX .

To quote the GNU C library manual: “This function was introduced in 4.3BSD and therefore is widely available.”¹

1. https://www.gnu.org/software/libc/manual/html_node/Finding-T...

Post reply on HN