HP C/iX Library Reference Manual (30026-90004)

Chapter 5 353
HP C/iX Library Function Descriptions
strtok
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);