函式
<cwchar>

wcsncpy

wchar_t* wcsncpy (wchar_t* destination, const wchar_t* source, size_t num);
從寬字串複製字元
source 的前 num 個字元複製到 destination。如果在複製 num 個字元之前遇到了 source C 寬字串的結尾(由 空寬字元 標識),則 destination 會被填充額外的 空寬字元,直到總共寫入了 num 個字元為止。

如果 source 的長度大於 num,則 destination 的末尾不會隱式地新增 空寬字元(因此,在這種情況下,destination 可能不是以空字元結尾的 C 寬字串)。

destinationsource 不得重疊(當出現重疊時,請參閱 wmemmove 以獲得更安全的選擇)。

這是 strncpy<cstring>)的 寬字元 等效函式。

引數

destination
指向要複製內容的目標陣列的指標。
source
要複製的 C 寬字串。
num
source 複製的最大寬字元數。
size_t 是一個無符號整數型別。

返回值

返回 destination

示例

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;
}

輸出

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


另見