Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strtok() and empty fields

Tags:

c

strtok

I am serializing some C structure to string and than deserializing it with strtok(). But, unfortunately, strtok() don't detect empty fields (eg 1:2::4).

Is there any alternative function?

like image 721
Marko Kevac Avatar asked Mar 04 '10 08:03

Marko Kevac


2 Answers

On linux there's strsep.

The strsep() function was introduced as a replacement for strtok(), since the latter cannot handle empty fields. However, strtok() conforms to C89/C99 and hence is more portable.

like image 199
Drakosha Avatar answered Oct 01 '22 03:10

Drakosha


You can use strchr (for just one delimiter character) or strcspn (for a group of possible delimiters) to find the next delimiter, process the token, and then just step one character forward. Do this in a loop and you have what you need.

like image 37
Tronic Avatar answered Oct 01 '22 03:10

Tronic