Re: strtok question
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Sat, 31 May 2008 12:45:18 -0400
Jens Thoms Toerring wrote:
Stu Cazzo <SCazzo@xxxxxxxxx> wrote:.... snip ...
.... snip ...
The splitString( string1 ); works as expected, 3 tokens are found.
The splitString( string2 ); does not work as I expected.
.... snip ...
Why does it not see the empty field for lineToken2?
Because that's not how strtok() works. The man page on my machine
for strtok() actually makes it rather clear:
Is there a better way to strip out the tokens for the case I have
where there is no space between my delimiter?
I guess you will have to write your own function for that, probably
repeatedly using strchr() or strstr().
Try this:
/* ------- file tknsplit.c ----------*/
#include "tknsplit.h"
/* copy over the next tkn from an input string, after
skipping leading blanks (or other whitespace?). The
tkn is terminated by the first appearance of tknchar,
or by the end of the source string.
The caller must supply sufficient space in tkn to
receive any tkn, Otherwise tkns will be truncated.
Returns: a pointer past the terminating tknchar.
This will happily return an infinity of empty tkns if
called with src pointing to the end of a string. Tokens
will never include a copy of tknchar.
A better name would be "strtkn", except that is reserved
for the system namespace. Change to that at your risk.
released to Public Domain, by C.B. Falconer.
Published 2006-02-20. Attribution appreciated.
Revised 2006-06-13 2007-05-26 (name)
*/
const char *tknsplit(const char *src, /* Source of tkns */
char tknchar, /* tkn delimiting char */
char *tkn, /* receiver of parsed tkn */
size_t lgh) /* length tkn can receive */
/* not including final '\0' */
{
if (src) {
while (' ' == *src) src++;
while (*src && (tknchar != *src)) {
if (lgh) {
*tkn++ = *src;
--lgh;
}
src++;
}
if (*src && (tknchar == *src)) src++;
}
*tkn = '\0';
return src;
} /* tknsplit */
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
.
- References:
- strtok question
- From: Stu Cazzo
- Re: strtok question
- From: Jens Thoms Toerring
- strtok question
- Prev by Date: Re: function
- Next by Date: Re: private functions
- Previous by thread: Re: strtok question
- Next by thread: private functions
- Index(es):
Relevant Pages
|