errno_t wcsncpy_s( wchar_t *restrict dest, rsize_t destsz,
const wchar_t *restrict src, rsize_t count);
1) Copies at most count
characters of the wide string pointed to by src
(including the terminating null wide character) to wide character array pointed to by dest
.
If count
is reached before the entire string src
was copied, the resulting wide character array is not null-terminated.
If, after copying the terminating null wide character from src
, count
is not reached, additional null wide characters are written to dest
until the total of count
characters have been written.
If the strings overlap, the behavior is undefined.
2)Same as
(1), except that the function does not continue writing zeroes into the destination array to pad up to
count
, it stops after writing the terminating null character (if there was no null in the source, it writes one at
dest[count]and then stops). Also, the following errors are detected at runtime and call the currently installed
constraint handlerfunction:
src
or dest
is a null pointerdestsz
or count
is zero or greater than RSIZE_MAX/sizeof(wchar_t)count
is greater or equal destsz
, but destsz
is less or equal wcsnlen_s(src, count), in other words, truncation would occurwcsncpy_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 constant 1 before including <wchar.h>.
1) returns a copy of dest
2) returns zero on success, returns non-zero on error. Also, on error, writes L'\0' to dest[0] (unless dest
is a null pointer or destsz
is zero or greater than RSIZE_MAX/sizeof(wchar_t)) and may clobber the rest of the destination array with unspecified values.
In typical usage, count
is the number of elements in the destination array.
Although truncation to fit the destination buffer is a security risk and therefore a runtime constraints violation for wcsncpy_s
, it is possible to get the truncating behavior by specifying count
equal to the size of the destination array minus one: it will copy the first count
wide characters and append the null wide terminator as always: wcsncpy_s(dst, sizeof dst / sizeof *dst, src, (sizeof dst / sizeof *dst)-1);
#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 "); } }
Possible output:
The contents of dest are: ã ã \0 \0 ã \0[edit] References
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4