Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      strtok, strtok_s

      From cppreference.com
      <c‎ |string‎ |byte
       
       
       
       
      Defined in header<string.h>
      (1)
      char* strtok(char* str,constchar* delim);
      (until C99)
      char* strtok(char*restrict str,constchar*restrict delim);
      (since C99)
      char* strtok_s(char*restrict str, rsize_t*restrict strmax,
                     constchar*restrict delim,char**restrict ptr);
      (2)(since C11)

      Tokenizes a null-terminated byte string.

      1) A sequence of calls tostrtok breaks the string pointed to bystr into a sequence of tokens, each of which is delimited by a character from the string pointed to bydelim. Each call in the sequence has asearch target :
      • Ifstr is non-null, the call is thefirst call in the sequence. The search target is null-terminated byte string pointed to bystr.
      • Ifstr is null, the call is one of thesubsequent calls in the sequence. The search target is determined by the previous call in the sequence.
      Each call in the sequence searches the search target for the first character that isnot contained in theseparator string pointed to bydelim, the separator string can be different from call to call.
      • If no such character is found, then there are no tokens in the search target. The search target for the next call in the sequence is unchanged.[1]
      • If such a character is found, it is the start of the current token.strtok then searches from there for the first character that is contained in the separator string.
        • If no such character is found, the current token extends to the end of search target. The search target for the next call in the sequence is an empty string.[2]
        • If such a character is found, it is overwritten by a null character, which terminates the current token. The search target for the next call in the sequence starts from the following character.
      Ifstr ordelim is not a pointer to a null-terminated byte string, the behavior is undefined.
      2) Same as(1), except for the following differences:
      • In each call, writes the number of characters left to see instr into*strmax and writes the tokenizer's internal state into*ptr.
      • Subsequent calls in the sequence must passstrmax andptr with the values stored by the previous call.
      • 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.
        • *ptr is a null pointer for a subsequent call in the sequence.
        • *strmax is greater thanRSIZE_MAX.
        • The end of the token found does not occur within the first*s1max characters of the search target.
      If bothstr points to a character array which lacks the null character andstrmax points to a value which is greater than the size of that character array, the behavior is undefined.
      As with all bounds-checked functions,strtok_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<string.h>.
      1. A token may still be formed in a subsequent call with a different separator string.
      2. No more tokens can be formed in subsequent calls.

      Contents

      [edit]Parameters

      str - pointer to the null-terminated byte string to tokenize
      delim - pointer to the null-terminated byte string identifying delimiters
      strmax - pointer to an object which initially holds the size ofstr:strtok_s stores the number of characters that remain to be examined
      ptr - pointer to an object of typechar*, which is used bystrtok_s to store its internal state

      [edit]Return value

      1) Returns a pointer to the first character of the next token, or a null pointer if there is no token.
      2) Returns a pointer to the first character of the next token, or a null pointer if there isno token or there is a runtime-constraint violation.

      [edit]Note

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

      Each call tostrtok modifies a static variable: is not thread safe.

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

      Thestrtok_s function differs from the POSIXstrtok_r function by guarding against storing outside of the string being tokenized, and by checking runtime constraints. The Microsoft CRTstrtok_s signature matches this POSIXstrtok_r definition, not the C11strtok_s.

      [edit]Example

      Run this code
      #define __STDC_WANT_LIB_EXT1__ 1#include <stdio.h>#include <string.h> int main(void){char input[]="A bird came down the walk";printf("Parsing the input string '%s'\n", input);char* token= strtok(input," ");while(token){puts(token);        token= strtok(NULL," ");} printf("Contents of the input string now: '");for(size_t n=0; n<sizeof input;++n)        input[n]?putchar(input[n]):fputs("\\0",stdout);puts("'"); #ifdef __STDC_LIB_EXT1__char str[]="A bird came down the walk";    rsize_t strmax=sizeof str;constchar* delim=" ";char* next_token;printf("Parsing the input string '%s'\n", str);    token= strtok_s(str,&strmax, delim,&next_token);while(token){puts(token);        token= strtok_s(NULL,&strmax, delim,&next_token);} printf("Contents of the input string now: '");for(size_t n=0; n<sizeof str;++n)        str[n]?putchar(str[n]):fputs("\\0",stdout);puts("'");#endif}

      Possible output:

      Parsing the input string 'A bird came down the walk'AbirdcamedownthewalkContents of the input string now: 'A\0bird\0came\0down\0the\0walk\0'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

      • C23 standard (ISO/IEC 9899:2024):
      • 7.24.5.8 The strtok function (p: TBD)
      • K.3.7.3.1 The strtok_s function (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.24.5.8 The strtok function (p: TBD)
      • K.3.7.3.1 The strtok_s function (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.24.5.8 The strtok function (p: 369-370)
      • K.3.7.3.1 The strtok_s function (p: 620-621)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.21.5.8 The strtok function (p: 332-333)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.11.5.8 The strtok function

      [edit]See also

      finds the first location of any character in one string, in another string
      (function)[edit]
      returns the length of the maximum initial segment that consists
      of only the characters not found in another byte string
      (function)[edit]
      returns the length of the maximum initial segment that consists
      of only the characters found in another byte string
      (function)[edit]
      (C95)(C11)
      finds the next token in a wide string
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/string/byte/strtok&oldid=180884"

      [8]ページ先頭

      ©2009-2025 Movatter.jp