strncpy_s、 _strncpy_s_l、 wcsncpy_s、 _wcsncpy_s_l、 _mbsncpy_s、 _mbsncpy_s_l

一個字串的字元複製到另一個。這些是 strncpy、 _strncpy_l、 wcsncpy、 _wcsncpy_l、 _mbsncpy、 _mbsncpy_l 的安全性增強版本,如 安全性功能,則在 CRT 中 中所述。

重要事項重要事項

_mbsncpy_s 和 _mbsncpy_s_l 不能用於 Windows 執行階段執行的應用程式。如需詳細資訊,請參閱 CRT 函式不支援使用 /ZW

errno_t strncpy_s(
   char *strDest,
   size_t numberOfElements,
   const char *strSource,
   size_t count
);
errno_t _strncpy_s_l(
   char *strDest,
   size_t numberOfElements,
   const char *strSource,
   size_t count,
   _locale_t locale
);
errno_t wcsncpy_s(
   wchar_t *strDest,
   size_t numberOfElements,
   const wchar_t *strSource,
   size_t count 
);
errno_t _wcsncpy_s_l(
   wchar_t *strDest,
   size_t numberOfElements,
   const wchar_t *strSource,
   size_t count,
   _locale_t locale
);
errno_t _mbsncpy_s(
   unsigned char *strDest,
   size_t numberOfElements,
   const unsigned char *strSource,
   size_t count 
);
errno_t _mbsncpy_s_l(
   unsigned char *strDest,
   size_t numberOfElements,
   const unsigned char *strSource,
   size_t count,
   locale_t locale
);
template <size_t size>
errno_t strncpy_s(
   char (&strDest)[size],
   const char *strSource,
   size_t count
); // C++ only
template <size_t size>
errno_t _strncpy_s_l(
   char (&strDest)[size],
   const char *strSource,
   size_t count,
   _locale_t locale
); // C++ only
template <size_t size>
errno_t wcsncpy_s(
   wchar_t (&strDest)[size],
   const wchar_t *strSource,
   size_t count 
); // C++ only
template <size_t size>
errno_t _wcsncpy_s_l(
   wchar_t (&strDest)[size],
   const wchar_t *strSource,
   size_t count,
   _locale_t locale
); // C++ only
template <size_t size>
errno_t _mbsncpy_s(
   unsigned char (&strDest)[size],
   const unsigned char *strSource,
   size_t count 
); // C++ only
template <size_t size>
errno_t _mbsncpy_s_l(
   unsigned char (&strDest)[size],
   const unsigned char *strSource,
   size_t count,
   locale_t locale
); // C++ only

參數

  • strDest
    目的資料。

  • numberOfElements
    目的資料的大小,以字元為單位)。

  • strSource
    來源字串。

  • count
    要複製的字元數或 _TRUNCATE

  • locale
    使用的地區設定。

傳回值

零,如果成功,則為 STRUNCATE ,如果攔截發生,否則為錯誤碼。

錯誤情況

strDest

numberOfElements

strSource

傳回值

strDest的內容。

NULL

any

any

EINVAL

無法修改

any

any

NULL

EINVAL

strDest[0 個] 設定為 0

any

0

any

EINVAL

無法修改

不是 NULL

太小

any

ERANGE

strDest[0 個] 設定為 0

備註

這些函式嘗試複製 strSource 第一個 D 字元到 strDest, D 為較小者 count 和 strSource的長度。如果這些 D 字元在大小中適當值會測量為 numberOfElements) 的 strDest (和仍然保留 null 結束字元的空間,則這些字元複製,並且結束的 null 附加;否則, strDest[0] 設定為 Null 字元和無效的參數叫用處理常式,如 參數驗證中所述。

不會對上述區段。如果 count 是 _TRUNCATE,則相同 strSource 像將放入 strDest 複製,同時保留永遠附加的結尾的 NULL 時的空間。

例如:

char dst[5];

strncpy_s(dst, 5, "a long string", 5);

表示我們要求 strncpy_s 的複製五個字元讀入緩衝區五個位元組;這不會保留 null 結束字元的空間,所以 strncpy_s 歸零字串並告知無效的參數處理常式。

如果攔截行為是必要的,請使用 _TRUNCATE 或size (– 1):

strncpy_s(dst, 5, "a long string", _TRUNCATE);

strncpy_s(dst, 5, "a long string", 4);

請注意不同,則為 strncpy,如果 count 大於 strSource的長度,目的是非填補字串是以 Null 字元長度 count。

如果來源和目的資料重疊, strncpy_s 行為是未定義。

如果 strDest 或 strSource 是 NULL,或者 numberOfElements 是 0,無效的參數處理常式被叫用。如果允許繼續執行,此函式回傳 EINVAL 並設置 errno 為 EINVAL 。

wcsncpy_s 和 _mbsncpy_s 是 strncpy_s 的寬字元和多位元組字元版本。引數和傳回值 wcsncpy_s 和 mbsncpy_s跟著變更。這六個函式另有相同的行為。

輸出值受地區設定的LC_CTYPE 分類設定所影響。如需詳細資訊,請參閱 setlocale 。這些函式沒有以 _l 後綴的版本在這些地區相依的行為上使用目前的地區設定,而以 _l 後綴版本除了它們會使用傳入的地區設定參數之外運作相同。如需詳細資訊,請參閱地區設定

在 C++ 中,使用這些函式由範本多載簡化;多載會推斷緩衝區長度 (自動排除指定大小引數),也可以用它們較新,安全對應自動取代舊,不安全的函式。如需詳細資訊,請參閱安全範本多載

這些函式的偵錯版本會先填入 0xFD 緩衝區。若要停用此行為,請使用 _CrtSetDebugFillThreshold

泛用文字常式對應

TCHAR.H 常式

未定義 _UNICODE & _MBCS

已定義 _MBCS

已定義 _UNICODE

_tcsncpy_s

strncpy_s

_mbsnbcpy_s

wcsncpy_s

_tcsncpy_s_l

_strncpy_s_l

_mbsnbcpy_s_l

_wcsncpy_s_l

注意事項注意事項

_strncpy_s_l、 _wcsncpy_s_l 和 _mbsncpy_s_l 沒有地區設定相關屬性和提供 _tcsncpy_s_l 並不適合直接呼叫。

需求

程序

必要的標頭檔

strncpy_s, _strncpy_s_l

<string.h>

wcsncpy_s, _wcsncpy_s_l

<string.h> 或 <wchar.h>

_mbsncpy_s, _mbsncpy_s_l

<mbstring.h>

如需其他相容性資訊,請參閱入門介紹中的 相容性 (Compatibility)

範例

// crt_strncpy_s_1.cpp
// compile with: /MTd

// these #defines enable secure template overloads
// (see last part of Examples() below)
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1 
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crtdbg.h>  // For _CrtSetReportMode
#include <errno.h>

// This example uses a 10-byte destination buffer.

errno_t strncpy_s_tester( const char * src,
                          int count )
{
   char dest[10];

   printf( "\n" );

   if ( count == _TRUNCATE )
      printf( "Copying '%s' to %d-byte buffer dest with truncation semantics\n",
               src, _countof(dest) );
   else
      printf( "Copying %d chars of '%s' to %d-byte buffer dest\n",
              count, src, _countof(dest) );

   errno_t err = strncpy_s( dest, _countof(dest), src, count );

   printf( "    new contents of dest: '%s'\n", dest );

   return err;
}


void Examples()
{
   strncpy_s_tester( "howdy", 4 );
   strncpy_s_tester( "howdy", 5 );
   strncpy_s_tester( "howdy", 6 );

   printf( "\nDestination buffer too small:\n" );
   strncpy_s_tester( "Hi there!!", 10 );

   printf( "\nTruncation examples:\n" );

   errno_t err = strncpy_s_tester( "How do you do?", _TRUNCATE );
   printf( "    truncation %s occur\n", err == STRUNCATE ? "did"
                                                       : "did not" );

   err = strncpy_s_tester( "Howdy.", _TRUNCATE );
   printf( "    truncation %s occur\n", err == STRUNCATE ? "did"
                                                       : "did not" );

   printf( "\nSecure template overload example:\n" );

   char dest[10];
   strncpy( dest, "very very very long", 15 );
   // With secure template overloads enabled (see #defines at
   // top of file), the preceding line is replaced by
   //    strncpy_s( dest, _countof(dest), "very very very long", 15 );
   // Instead of causing a buffer overrun, strncpy_s invokes
   // the invalid parameter handler.
   // If secure template overloads were disabled, strncpy would
   // copy 15 characters and overrun the dest buffer.
   printf( "    new contents of dest: '%s'\n", dest );
}

void myInvalidParameterHandler(
   const wchar_t* expression,
   const wchar_t* function, 
   const wchar_t* file, 
   unsigned int line, 
   uintptr_t pReserved)
{
   wprintf(L"Invalid parameter handler invoked: %s\n", expression);
}

int main( void )
{
   _invalid_parameter_handler oldHandler, newHandler;

   newHandler = myInvalidParameterHandler;
   oldHandler = _set_invalid_parameter_handler(newHandler);
   // Disable the message box for assertions.
   _CrtSetReportMode(_CRT_ASSERT, 0);

   Examples();
}
  
// crt_strncpy_s_2.c
// contrasts strncpy and strncpy_s

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   char a[20] = "test";
   char s[20];

   // simple strncpy usage:

   strcpy_s( s, 20, "dogs like cats" );
   printf( "Original string:\n   '%s'\n", s );

   // Here we can't use strncpy_s since we don't 
   // want null termination
   strncpy( s, "mice", 4 );
   printf( "After strncpy (no null-termination):\n   '%s'\n", s );
   strncpy( s+5, "love", 4 );
   printf( "After strncpy into middle of string:\n   '%s'\n", s );

   // If we use strncpy_s, the string is terminated 
   strncpy_s( s, _countof(s), "mice", 4 );
   printf( "After strncpy_s (with null-termination):\n   '%s'\n", s );

}
  

.NET Framework 對等用法

System::String::Copy

請參閱

參考

字串操作 (CRT)

地區設定

多位元組字元序列的轉譯工作

_mbsnbcpy _mbsnbcpy_l

strcat_s,wcscat_s _mbscat_s

strcmp,wcscmp _mbscmp

strcpy_s,wcscpy_s _mbscpy_s

strncat_s、 _strncat_s_l、 wcsncat_s、 _wcsncat_s_l、 _mbsncat_s、 _mbsncat_s_l

strncmp、 wcsncmp、 _mbsncmp、 _mbsncmp_l

_strnicmp、 _wcsnicmp、 _mbsnicmp、 _strnicmp_l、 _wcsnicmp_l、 _mbsnicmp_l

strrchr、 wcsrchr、 _mbsrchr、 _mbsrchr_l

_strset、 _strset_l、 _wcsset、 _wcsset_l、 _mbsset、 _mbsset_l

strspn、 wcsspn、 _mbsspn、 _mbsspn_l