Re: strtok() and EOL
- From: Ben Pfaff <blp@xxxxxxxxxxxxxxx>
- Date: Tue, 29 Nov 2005 09:44:13 -0800
Fernando Barsoba <fbarsoba@xxxxxxxxxxx> writes:
> I'm using strtok() in the following way:
I don't generally recommend using strtok(). It has at least
these problems:
* It merges adjacent delimiters. If you use a comma as
your delimiter, then "a,,b,c" is three tokens, not
four. This is often the wrong thing to do. In fact,
it is only the right thing to do, in my experience,
when the delimiter set is limited to white space.
* The identity of the delimiter is lost, because it is
changed to a null terminator.
* It modifies the string that it tokenizes. This is bad
because it forces you to make a copy of the string if
you want to use it later. It also means that you can't
tokenize a string literal with it; this is not
necessarily something you'd want to do all the time but
it is surprising.
* It can only be used once at a time. If a sequence of
strtok() calls is ongoing and another one is started,
the state of the first one is lost. This isn't a
problem for small programs but it is easy to lose track
of such things in hierarchies of nested functions in
large programs. In other words, strtok() breaks
encapsulation.
--
"You call this a *C* question? What the hell are you smoking?" --Kaz
.
- References:
- strtok() and EOL
- From: Fernando Barsoba
- strtok() and EOL
- Prev by Date: Re: Advanced C
- Next by Date: Re: C function for returning number of digits?
- Previous by thread: Re: strtok() and EOL
- Next by thread: More questions on realloc
- Index(es):
Relevant Pages
|