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
|
<h1 id="firstHeading" class="firstHeading">wcstok, wcstok_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><wchar.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-since-c95 t-until-c99"> <td> <pre data-language="c">wchar_t* wcstok( wchar_t* str, const wchar_t* delim, wchar_t **ptr );</pre>
</td> <td> <span class="t-mark-rev t-since-c95">(since C95)</span> <br><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">wchar_t *wcstok(wchar_t * restrict str, const wchar_t * restrict delim,
wchar_t **restrict ptr);</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">wchar_t *wcstok_s( wchar_t *restrict str, rsize_t *restrict strmax,
const wchar_t *restrict delim, wchar_t **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 wide string pointed to by <code>str</code>. The separator characters are identified by null-terminated wide string pointed to by <code>delim</code>.</div> <div class="t-li1">
This function is designed to be called multiples times to obtain successive tokens from the same string.</div> <ul>
<li> If <code>str <span class="sy3">!</span><span class="sy1">=</span> <a href="http://en.cppreference.com/w/c/types/NULL"><span class="kw103">NULL</span></a></code>, the call is treated as the first call to <code>wcstok</code> for this particular wide string. The function searches for the first wide character which is <i>not</i> contained in <code>delim</code>. </li>
<li> If no such wide character was found, there are no tokens in <code>str</code> at all, and the function returns a null pointer. </li>
<li> If such wide character was found, it is the <i>beginning of the token</i>. The function then searches from that point on for the first wide character that <i>is</i> contained in <code>delim</code>. </li>
<ul>
<li> If no such wide character was found, <code>str</code> has only one token, and future calls to <code>wcstok</code> will return a null pointer </li>
<li> If such wide character was found, it is <i>replaced</i> by the null wide character <code>L'\0'</code> and the parser state (typically a pointer to the following wide character) is stored in the user-provided location <code>*ptr</code>. </li>
</ul>
<li> The function then returns the pointer to the beginning of the token </li>
<li> If <code>str <span class="sy1">==</span> <a href="http://en.cppreference.com/w/c/types/NULL"><span class="kw103">NULL</span></a></code>, the call is treated as a subsequent call to <code>wcstok</code>: the function continues from where it left in the previous invocation with the same <code>*ptr</code>. The behavior is the same as if the pointer to the wide character that follows the last detected token is passed as <code>str</code>. </li>
</ul> <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>. Repeat calls (with null <code>str</code>) must pass both <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> <dl>
<dd>
<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/sizeof(wchar_t)</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> </dd>
<dd>As all bounds-checked functions, <code>wcstok_s</code> is 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 <code>wchar.h</code>.</dd>
</dl>
</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 wide string to tokenize </td>
</tr> <tr class="t-par"> <td> delim </td> <td> - </td> <td> pointer to the null-terminated wide string identifying delimiters </td>
</tr> <tr class="t-par"> <td> ptr </td> <td> - </td> <td> pointer to an object of type <code>wchar_t*</code>, which is used by both <code>wcstok</code> and <code>wcstok_s</code> to store the internal state of the parser </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>: wcstok_s stores the number of characters that remain to be examined </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>Returns pointer to the beginning of the next token or null pointer if there are no more tokens.</p>
<h3 id="Note"> Note</h3> <p>This function is destructive: it writes the <code>L'\0'</code> characters in the elements of the string <code>str</code>. In particular, a wide string literal cannot be used as the first argument of <code>wcstok</code>.</p>
<p>Unlike <code><a href="../byte/strtok" title="c/string/byte/strtok">strtok</a></code>, <code>wcstok</code> does not update static storage: it stores the parser state in the user-provided location.</p>
<p>Unlike most other tokenizers, the delimiters in <code>wcstok</code> can be different for each subsequent token, and can even depend on the contents of the previous tokens.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <wchar.h>
#include <stdio.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("'");
}</pre></div> <p>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'</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.29.4.5.7 The wcstok function (p: 437-438) </li>
<li> K.3.9.2.3.1 The wcstok_s function (p: 645-646) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.24.4.5.7 The wcstok function (p: 383-384) </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="../byte/strtok" title="c/string/byte/strtok"> <span class="t-lines"><span>strtok</span><span>strtok_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> finds the next token in a byte 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/wide/wcstok" title="cpp/string/wide/wcstok">C++ documentation</a></span> for <code>wcstok</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/wide/wcstok" class="_attribution-link">https://en.cppreference.com/w/c/string/wide/wcstok</a>
</p>
</div>
|