HP 3000 Manuals

strtok [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation


HP C/iX Library Reference Manual

strtok 

Divides string s1 into zero or more tokens.  The token separators consist
of any characters contained in string s2.

Syntax 

     #include <string.h>
     char *strtok(char *s1, const char *s2);

Parameters 

s1            A pointer to a string with zero or more tokens.

s2            A pointer to a character string with token delimiters.

Return Values 

x             A pointer to the first character of a token.

NULL          No token found.

Description 

A token is a string of characters delimited by one or more token
delimiters.  In the strtok function, s1 is a character pointer to the
string that is to be broken up into tokens, and s2 is a character pointer
to a string consisting of characters to be treated as token separators.

The strtok function returns the next token from s1 each time it is
called.  The first time strtok is called, both s1 and s2 must be
specified.  On subsequent calls, s1 is not specified (a null pointer is
specified in its place).  The strtok function remembers the string from
call to call.  String s2 must be specified for each call, but need not
contain the same characters (token separators).

The strtok function returns a pointer to the beginning of the token, and
writes a null character into s1 immediately following the end of the
returned token, overwriting the token delimiter.  This function returns a
null pointer when no tokens remain.

Example 

This example assumes that you are reading lines from a file containing
several fields delimited by pound signs (#).  The following code could be
used to read the fields of each line:

        int count = 0;
        char *delims = "#", *token, *arg1, *strtok(), line[256];
        arg1 = line;
            :
        while((token = strtok(arg1, delims)) != NULL) {
           count++;
           printf("field %d: %s\n", count, token);
           arg1 = NULL;
        }

This code sees to it that strtok()'s first argument is null after the
first call.  Also, note that delims did not change from call to call, but
it could have.  This greatly increases the power of strtok, because it
enables you to change the token delimiters between calls.

See Also 

ANSI C 4.11.5.8, POSIX.1 8.1



MPE/iX 5.0 Documentation