wcstok() — Tokenize Wide-Character String

Format

#include <wchar.h>
wchar_t *wcstok(wchar_t *wcs1, const wchar_t *wcs2, wchar_t **ptr);

Language Level: ANSI

Threadsafe: Yes.

Wide Character Function: See Wide Characters for more information.

Description

The wcstok() function 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 the wcstok() function 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 the wcstok() function stores information necessary for it to continue scanning the same string.

When the wcstok() function is first called for the wide-character string wcs1, it searches for the first token in wcs1, skipping over leading delimiters. The wcstok() function returns a pointer to the first token. To read the next token from wcs1, call the wcstok() function with NULL as the first parameter (wcs1). This NULL parameter causes the wcstok() function to search for the next token in the previous token string. Each delimiter is replaced by a null character to end the token.

The wcstok() function 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.

Return Value

The 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, the wcstok() function returns a pointer to the next token in the string. When there are no more tokens, the wcstok() function returns NULL.

Example that uses wcstok()

This example uses the wcstok() function 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 = ''
          t = 'c'
          t = ''
   ********************************************************************/
}

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]