名稱空間
變體
操作

std::wcsncpy

來自 cppreference.com
< cpp‎ | string‎ | wide
在標頭檔案 <cwchar> 中定義
wchar_t* wcsncpy( wchar_t* dest, const wchar_t* src, std::size_t count );

最多複製 src 指向的寬字串的 count 個字元(包括終止空寬字元)到 dest 指向的寬字元陣列。

如果在複製整個字串 src 之前達到 count,則生成的寬字元陣列不會以空字元終止。

如果從 src 複製終止空寬字元後,count 尚未達到,則會向 dest 寫入額外的空寬字元,直到總共寫入 count 個字元。

如果字串重疊,則行為未定義。

目錄

[編輯] 引數

dest - 指向要複製到的寬字元陣列的指標
src - 指向要從中複製的寬字串的指標
count - 要複製的最大寬字元數

[編輯] 返回值

dest

[編輯] 注意

在典型用法中,count 是目標陣列的大小。

[編輯] 示例

#include <cwchar>
#include <iostream>
 
int main()
{
    const wchar_t src[] = L"hi";
    wchar_t dest[6] = {L'a', L'b', L'c', L'd', L'e', L'f'};
 
    std::wcsncpy(dest, src, 5); // this will copy 'hi' and repeat \0 three times
 
    std::wcout << "The contents of dest are: ";
    for (const wchar_t c : dest)
    {
        if (c)
            std::wcout << c << ' ';
        else
            std::wcout << "\\0" << ' ';
    }
    std::wcout << '\n';
}

輸出

The contents of dest are: h i \0 \0 \0 f

[編輯] 參閱

將一個寬字串複製到另一個寬字串
(函式) [編輯]
在兩個不重疊的陣列之間複製一定數量的寬字元
(函式) [編輯]
從一個字串複製一定數量的字元到另一個字串
(函式) [編輯]
C documentation for wcsncpy