Format
#include <wchar.h> wchar_t *wcstok(wchar_t *wcs1, const wchar_t *wcs2, wchar_t **ptr);
Language Level: ANSI 93, XPG4
wcstok reads wcs1 as a series of zero or more tokens
and wcs2 as the set of wide characters serving as
delimiters for the tokens in wcs1. A sequence of calls
to wcstok locates the tokens inside wcs1. The tokens
can be separated by one or more of the delimiters from wcs2.
The third argument points to a wide-character pointer that you
provide where wcstok stores information necessary for it to
continue scanning the same string.
When wcstok is first called for the wide-character string wcs1, it searches for the first token in wcs1, skipping over leading delimiters. wcstok returns a pointer to the first token.
To read the next token from wcs1, call wcstok with NULL as the first parameter (wcs1). This NULL parameter causes wcstok to search for the next token in the previous token string. Each delimiter is replaced by a null character to terminate the token.
wcstok always stores enough information in the pointer ptr so that subsequent calls, with NULL as the first parameter and the unmodified pointer value as the third, will start searching right after the previously returned token. You can change the set of delimiters (wcs2) from call to call.
The behavior of wcstok function is affected by the LC_CTYPE category of the current locale.
Return Value
wcstok function returns a pointer to the first wide
character of the token, or a null pointer if there is no token.
In later calls with the same token string, strtok returns a
pointer to the next token in the string. When there are no more
tokens, strtok returns NULL.
Example
This example uses wcstok to locate the tokens in the
wide-character string str1.
#include <stdio.h> #include <wchar.h>
int main(void)
{
static wchar_t str1[] = L"?a??b,,,#c";
static wchar_t str2[] = L"\t \t";
wchar_t *t, *ptr1, *ptr2;
t = wcstok(str1, L"?", &ptr1); /* t points to the token L"a" */
printf("t = '%ls'\n", t);
t = wcstok(NULL, L",", &ptr1); /* t points to the token L"?b" */
printf("t = '%ls'\n", t);
t = wcstok(str2, L" \t,", &ptr2); /* t is a null pointer */
printf("t = '%ls'\n", t);
t = wcstok(NULL, L"#,", &ptr1); /* t points to the token L"c" */
printf("t = '%ls'\n", t);
t = wcstok(NULL, L"?", &ptr1); /* t is a null pointer */
printf("t = '%ls'\n", t);
return 0;
/********************************************************************
The output should be similar to:
t = 'a'
t = '?b'
t = '(null)'
t = 'c'
t = '(null)'
********************************************************************/
}
![]()
strtok -- Tokenize String
<wchar.h>