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
|
<h1 id="firstHeading" class="firstHeading">wcsncpy, wcsncpy_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* wcsncpy( wchar_t* dest, const wchar_t* src, size_t count );</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 *wcsncpy( wchar_t *restrict dest, const wchar_t *restrict src, size_t count );</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">errno_t wcsncpy_s( wchar_t *restrict dest, rsize_t destsz,
const wchar_t *restrict src, rsize_t count);</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> Copies at most <code>count</code> characters of the wide string pointed to by <code>src</code> (including the terminating null wide character) to wide character array pointed to by <code>dest</code>. </div> <div class="t-li1">
If <code>count</code> is reached before the entire string <code>src</code> was copied, the resulting wide character array is not null-terminated.</div> <div class="t-li1">
If, after copying the terminating null wide character from <code>src</code>, <code>count</code> is not reached, additional null wide characters are written to <code>dest</code> until the total of <code>count</code> characters have been written.</div> <div class="t-li1">
If the strings overlap, the behavior is undefined.</div> <div class="t-li1">
<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that the function does not continue writing zeroes into the destination array to pad up to <code>count</code>, it stops after writing the terminating null character (if there was no null in the source, it writes one at <code>dest[count]</code> and then stops). 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: <dl>
<dd>
<ul>
<li> <code>src</code> or <code>dest</code> is a null pointer </li>
<li> <code>destsz</code> or <code>count</code> is zero or greater than <code>RSIZE_MAX/sizeof(wchar_t)</code> </li>
<li> <code>count</code> is greater or equal <code>destsz</code>, but <code>destsz</code> is less or equal <code>wcsnlen_s(src, count)</code>, in other words, truncation would occur </li>
<li> overlap would occur between the source and the destination strings </li>
</ul> </dd>
<dd>As with all bounds-checked functions, <code>wcsncpy_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="../wide" title="c/string/wide"><code><wchar.h></code></a>.</dd>
</dl>
</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> dest </td> <td> - </td> <td> pointer to the wide character array to copy to </td>
</tr> <tr class="t-par"> <td> src </td> <td> - </td> <td> pointer to the wide string to copy from </td>
</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> maximum number of wide characters to copy </td>
</tr> <tr class="t-par"> <td> destsz </td> <td> - </td> <td> the size of the destination buffer </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1">
<span class="t-li">1)</span> returns a copy of <code>dest</code>
</div> <div class="t-li1">
<span class="t-li">2)</span> returns zero on success, returns non-zero on error. Also, on error, writes <code>L'\0'</code> to <code>dest[0]</code> (unless <code>dest</code> is a null pointer or <code>destsz</code> is zero or greater than <code>RSIZE_MAX/sizeof(wchar_t)</code>) and may clobber the rest of the destination array with unspecified values.</div> <h3 id="Notes"> Notes</h3> <p>In typical usage, <code>count</code> is the number of elements in the destination array.</p>
<p>Although truncation to fit the destination buffer is a security risk and therefore a runtime constraints violation for <code>wcsncpy_s</code>, it is possible to get the truncating behavior by specifying <code>count</code> equal to the size of the destination array minus one: it will copy the first <code>count</code> wide characters and append the null wide terminator as always: <code>wcsncpy_s(dst, sizeof dst / sizeof *dst, src, (sizeof dst / sizeof *dst)-1);</code></p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(void)
{
const wchar_t src[] = L"わゐ";
wchar_t dest[6] = {L'あ', L'い', L'う', L'え', L'お'};
wcsncpy(dest, src, 4); // this will copy わゐ and repeat L'\0' two times
puts("The contents of dest are: ");
setlocale(LC_ALL, "en_US.utf8");
const long dest_size = sizeof dest / sizeof *dest;
for(wchar_t* p = dest; p-dest != dest_size; ++p) {
*p ? printf("%lc ", *p)
: printf("\\0 ");
}
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">The contents of dest are:
わ ゐ \0 \0 お \0</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.29.4.2.2 The wcsncpy function (p: 314) </li>
<li> K.3.9.2.1.2 The wcsncpy_s function (p: 464) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.29.4.2.2 The wcsncpy function (p: 431) </li>
<li> K.3.9.2.1.2 The wcsncpy_s function (p: 640-641) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.24.4.2.2 The wcsncpy function (p: 377) </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="wcscpy" title="c/string/wide/wcscpy"> <span class="t-lines"><span>wcscpy</span><span>wcscpy_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> copies one wide string to another <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="wmemcpy" title="c/string/wide/wmemcpy"> <span class="t-lines"><span>wmemcpy</span><span>wmemcpy_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> copies a certain amount of wide characters between two non-overlapping arrays <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../byte/strncpy" title="c/string/byte/strncpy"> <span class="t-lines"><span>strncpy</span><span>strncpy_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> copies a certain amount of characters from one string to another <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/wcsncpy" title="cpp/string/wide/wcsncpy">C++ documentation</a></span> for <code>wcsncpy</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/wcsncpy" class="_attribution-link">https://en.cppreference.com/w/c/string/wide/wcsncpy</a>
</p>
</div>
|