1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
<h1 id="firstHeading" class="firstHeading">strtok, strtok_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><string.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl-rev-aux"> <td></td> <td rowspan="3">(1)</td> <td></td> </tr> <tr class="t-dcl t-until-c99"> <td><pre data-language="c">char *strtok( char *str, const char *delim );</pre></td> <td><span class="t-mark-rev t-until-c99">(until C99)</span></td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">char *strtok( char *restrict str, const char *restrict delim );</pre>
</td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">char *strtok_s(char *restrict str, rsize_t *restrict strmax,
const char *restrict delim, char **restrict ptr);</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <div class="t-li1">
<span class="t-li">1)</span> Finds the next token in a null-terminated byte string pointed to by <code>str</code>. The separator characters are identified by null-terminated byte string pointed to by <code>delim</code>.</div> <div class="t-li1">
This function is designed to be called multiple times to obtain successive tokens from the same string.</div> <ul>
<li> If <code>str</code> is not a null pointer, the call is treated as the first call to <code>strtok</code> for this particular string. The function searches for the first character which is <i>not</i> contained in <code>delim</code>. </li>
<li> If no such character was found, there are no tokens in <code>str</code> at all, and the function returns a null pointer. </li>
<li> If such character was found, it is the <i>beginning of the token</i>. The function then searches from that point on for the first character that <i>is</i> contained in <code>delim</code>. </li>
<ul>
<li> If no such character was found, <code>str</code> has only one token, and future calls to <code>strtok</code> will return a null pointer </li>
<li> If such character was found, it is <i>replaced</i> by the null character <code>'\0'</code> and the pointer to the following character is stored in a static location for subsequent invocations. </li>
</ul>
<li> The function then returns the pointer to the beginning of the token </li>
<li> If <code>str</code> is a null pointer, the call is treated as a subsequent call to <code>strtok</code>: the function continues from where it left in previous invocation. The behavior is the same as if the previously stored pointer is passed as <code>str</code>. </li>
</ul> <div class="t-li1">
The behavior is undefined if either <code>str</code> or <code>delim</code> is not a pointer to a null-terminated byte string.</div> <div class="t-li1">
<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that on every step, writes the number of characters left to see in <code>str</code> into <code>*strmax</code> and writes the tokenizer's internal state to <code>*ptr</code>. Repeat calls (with null <code>str</code>) must pass <code>strmax</code> and <code>ptr</code> with the values stored by the previous call. Also, the following errors are detected at runtime and call the currently installed <a href="../../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function, without storing anything in the object pointed to by <code>ptr</code> <ul>
<li> <code>strmax</code>, <code>delim</code>, or <code>ptr</code> is a null pointer </li>
<li> on a non-initial call (with null <code>str</code>), <code>*ptr</code> is a null pointer </li>
<li> on the first call, <code>*strmax</code> is zero or greater than <code>RSIZE_MAX</code> </li>
<li> search for the end of a token reaches the end of the source string (as measured by the initial value of <code>*strmax</code>) without encountering the null terminator</li>
</ul>
</div> <div class="t-li1">
The behavior is undefined if both <code>str</code> points to a character array which lacks the null character and <code>strmax</code> points to a value which is greater than the size of that character array. As with all bounds-checked functions, <code>strtok_s</code> only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../byte" title="c/string/byte"><code><string.h></code></a>.</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> str </td> <td> - </td> <td> pointer to the null-terminated byte string to tokenize </td>
</tr> <tr class="t-par"> <td> delim </td> <td> - </td> <td> pointer to the null-terminated byte string identifying delimiters </td>
</tr> <tr class="t-par"> <td> strmax </td> <td> - </td> <td> pointer to an object which initially holds the size of <code>str</code>: strtok_s stores the number of characters that remain to be examined </td>
</tr> <tr class="t-par"> <td> ptr </td> <td> - </td> <td> pointer to an object of type <code>char*</code>, which is used by strtok_s to store its internal state </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>Returns pointer to the beginning of the next token or a null pointer if there are no more tokens.</p>
<h3 id="Note"> Note</h3> <p>This function is destructive: it writes the <code>'\0'</code> characters in the elements of the string <code>str</code>. In particular, a string literal cannot be used as the first argument of <code>strtok</code>.</p>
<p>Each call to <code>strtok</code> modifies a static variable: is not thread safe.</p>
<p>Unlike most other tokenizers, the delimiters in <code>strtok</code> can be different for each subsequent token, and can even depend on the contents of the previous tokens.</p>
<p>The <code>strtok_s</code> function differs from the POSIX <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtok.html"><code>strtok_r</code></a> function by guarding against storing outside of the string being tokenized, and by checking runtime constraints. The Microsoft CRT <code>strtok_s</code> signature matches this POSIX <code>strtok_r</code> definition, not the C11 <code>strtok_s</code>.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>
#include <stdio.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;
const char *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
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Parsing the input string 'A bird came down the walk'
A
bird
came
down
the
walk
Contents of the input string now: 'A\0bird\0came\0down\0the\0walk\0'
Parsing the input string 'A bird came down the walk'
A
bird
came
down
the
walk
Contents of the input string now: 'A\0bird\0came\0down\0the\0walk\0'</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.24.5.8 The strtok function (p: 369-370) </li>
<li> K.3.7.3.1 The strtok_s function (p: 620-621) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.21.5.8 The strtok function (p: 332-333) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.11.5.8 The strtok function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="strpbrk" title="c/string/byte/strpbrk"> <span class="t-lines"><span>strpbrk</span></span></a></div> </td> <td> finds the first location of any character in one string, in another string <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="strcspn" title="c/string/byte/strcspn"> <span class="t-lines"><span>strcspn</span></span></a></div> </td> <td> returns the length of the maximum initial segment that consists <br> of only the characters not found in another byte string <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="strspn" title="c/string/byte/strspn"> <span class="t-lines"><span>strspn</span></span></a></div> </td> <td> returns the length of the maximum initial segment that consists <br> of only the characters found in another byte string <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../wide/wcstok" title="c/string/wide/wcstok"> <span class="t-lines"><span>wcstok</span><span>wcstok_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> finds the next token in a wide string <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/byte/strtok" title="cpp/string/byte/strtok">C++ documentation</a></span> for <code>strtok</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/string/byte/strtok" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strtok</a>
</p>
</div>
|