Re: strtok question



Jens Thoms Toerring wrote:
Stu Cazzo <SCazzo@xxxxxxxxx> wrote:

.... 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:

.... snip ...

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 **
.



Relevant Pages

  • Re: strtok and strtok_r
    ... As I know strtok_r is re-entrant version of strtok. ... skipping leading blanks. ... or by the end of the source string. ... receive any tkn, ...
    (comp.lang.c)
  • Re: Any way to take a word as input from stdin ?
    ... My dissatisfaction with strtok() is ... string literal, or data you want to keep. ... /* copy over the next tkn from an input string, ... a pointer past the terminating tknchar. ...
    (comp.lang.c)
  • Re: The Philosophy of Programming?
    ... less trivial to ensure that the string length has not been altered. ... > lowers psychological complexity for most intelligent readers. ... For most intelligent programmers, the laziness principal applies. ...
    (comp.programming)
  • Re: A C++ Whishlist
    ... > people from inclusion in a standard. ... > creating their own string class. ... >>don't want an ever increasing size of exception specification on each ...
    (comp.lang.cpp)
  • Re: Hash functions (was: Maximum String size in Java?)
    ... snip ... ... Inserted with sx31hsh and ssfh_ph in 0.385 secs ... your string library has problems with them speaks for itself. ... published your SFH code with the shifted and undersized ints. ...
    (comp.programming)