function
<cwchar>

wcsncpy

wchar_t* wcsncpy (wchar_t* destination, const wchar_t* source, size_t num);
Copy characters from wide string
Copies the first num characters of source to destination. If the end of the source C wide string (which is signaled by a null wide character) is found before num characters have been copied, destination is padded with additional null wide characters until a total of num characters have been written to it.

No null wide character is implicitly appended at the end of destination if source is longer than num (thus, in this case, destination may not be a null terminated C wide string).

destination and source shall not overlap (see wmemmove for a safer alternative when overlapping).

This is the wide character equivalent of strncpy (<cstring>).

Parameters

destination
Pointer to the destination array where the content is to be copied.
source
C wide string to be copied.
num
Maximum number of wide characters to be copied from source.
size_t is an unsigned integral type.

Return Value

destination is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* wcsncpy example */
#include <wchar.h>

int main ()
{
  wchar_t wcs1[] = L"To be or not to be";
  wchar_t wcs2[40];
  wchar_t wcs3[40];

  /* copy to sized buffer (overflow safe): */
  wcsncpy ( wcs2, wcs1, 40 );

  /* partial copy (only 5 characters): */
  wcsncpy ( wcs3, wcs2, 5 );
  wcs3[5] = L'\0';   /* null character manually added */

  wprintf (L"%ls\n%ls\n%ls\n",wcs1,wcs2,wcs3);

  return 0;
}


Output:

To be or not to be
To be or not to be
To be 

See also