Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      wcstok, wcstok_s

      From cppreference.com
      <c‎ |string‎ |wide
       
       
       
      Null-terminated wide strings
       
      Defined in header<wchar.h>
      (1)
      wchar_t* wcstok(wchar_t* str,constwchar_t* delim,wchar_t** ptr);
      (since C95)
      (until C99)
      wchar_t* wcstok(wchar_t*  restrict str,constwchar_t*restrict delim,
                       wchar_t**restrict ptr);
      (since C99)
      wchar_t* wcstok_s(wchar_t*restrict str, rsize_t*restrict strmax,
                         constwchar_t*restrict delim,wchar_t**restrict ptr);
      (2)(since C11)
      1) Finds the next token in a null-terminated wide string pointed to bystr. The separator characters are identified by null-terminated wide string pointed to bydelim.
      This function is designed to be called multiples times to obtain successive tokens from the same string.
      • Ifstr!=NULL, the call is treated as the first call towcstok for this particular wide string. The function searches for the first wide character which isnot contained indelim.
      • If no such wide character was found, there are no tokens instr at all, and the function returns a null pointer.
      • If such wide character was found, it is thebeginning of the token. The function then searches from that point on for the first wide character thatis contained indelim.
      • If no such wide character was found,str has only one token, and future calls towcstok will return a null pointer
      • If such wide character was found, it isreplaced by the null wide characterL'\0' and the parser state (typically a pointer to the following wide character) is stored in the user-provided location*ptr.
      • The function then returns the pointer to the beginning of the token
      • Ifstr==NULL, the call is treated as a subsequent call towcstok: the function continues from where it left in the previous invocation with the same*ptr. The behavior is the same as if the pointer to the wide character that follows the last detected token is passed asstr.
      2) Same as(1), except that on every step, writes the number of characters left to see instr into*strmax. Repeat calls (with nullstr) must pass bothstrmax andptr with the values stored by the previous call. Also, the following errors are detected at runtime and call the currently installedconstraint handler function, without storing anything in the object pointed to byptr
      • strmax,delim, orptr is a null pointer
      • on a non-initial call (with nullstr),*ptr is a null pointer
      • on the first call,*strmax is zero or greater thanRSIZE_MAX/sizeof(wchar_t)
      • search for the end of a token reaches the end of the source string (as measured by the initial value of*strmax) without encountering the null terminator
      As all bounds-checked functions,wcstok_s is only guaranteed to be available if__STDC_LIB_EXT1__ is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__ to the integer constant1 before including<wchar.h>.

      Contents

      [edit]Parameters

      str - pointer to the null-terminated wide string to tokenize
      delim - pointer to the null-terminated wide string identifying delimiters
      ptr - pointer to an object of typewchar_t*, which is used by bothwcstok andwcstok_s to store the internal state of the parser
      strmax - pointer to an object which initially holds the size ofstr: wcstok_s stores the number of characters that remain to be examined

      [edit]Return value

      Returns pointer to the beginning of the next token or null pointer if there are no more tokens.

      [edit]Note

      This function is destructive: it writes theL'\0' characters in the elements of the stringstr. In particular, a wide string literal cannot be used as the first argument ofwcstok.

      Unlikestrtok,wcstok does not update static storage: it stores the parser state in the user-provided location.

      Unlike most other tokenizers, the delimiters inwcstok can be different for each subsequent token, and can even depend on the contents of the previous tokens.

      The implementation ofwcstok_s in theWindows CRT is incompatible with the C standard, it is merely an alias forwcstok.

      [edit]Example

      Run this code
      #include <stdio.h>#include <wchar.h> int main(void){wchar_t input[]= L"A bird came down the walk";printf("Parsing the input string '%ls'\n", input);wchar_t* buffer;wchar_t* token= wcstok(input, L" ",&buffer);while(token){printf("%ls\n", token);        token= wcstok(NULL, L" ",&buffer);} printf("Contents of the input string now: '");for(size_t n=0; n<sizeof input/sizeof*input;++n)        input[n]?printf("%lc", input[n]):printf("\\0");puts("'");}

      Output:

      Parsing the input string 'A bird came down the walk'AbirdcamedownthewalkContents of the input string now: 'A\0bird\0came\0down\0the\0walk\0'

      [edit]References

      • C11 standard (ISO/IEC 9899:2011):
      • 7.29.4.5.7 The wcstok function (p: 437-438)
      • K.3.9.2.3.1 The wcstok_s function (p: 645-646)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.24.4.5.7 The wcstok function (p: 383-384)

      [edit]See also

      finds the next token in a byte string
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/string/wide/wcstok&oldid=168847"

      [8]ページ先頭

      ©2009-2025 Movatter.jp