Share via


strcat_s, wcscat_s, _mbscat_s, _mbscat_s_l

附加字串。 這些版本的 strcatwcscat_mbscat 具有安全性增強功能,如 CRT 的安全性功能中所述。

重要

在 Windows 執行階段中執行的應用程式中無法使用 _mbscat_s_mbscat_s_l。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 Windows 平台應用程式中不支援的 CRT 函式)。

語法

errno_t strcat_s(
   char *strDestination,
   size_t numberOfElements,
   const char *strSource
);
errno_t wcscat_s(
   wchar_t *strDestination,
   size_t numberOfElements,
   const wchar_t *strSource
);
errno_t _mbscat_s(
   unsigned char *strDestination,
   size_t numberOfElements,
   const unsigned char *strSource
);
errno_t _mbscat_s_l(
   unsigned char *strDestination,
   size_t numberOfElements,
   const unsigned char *strSource,
   _locale_t locale
);
template <size_t size>
errno_t strcat_s(
   char (&strDestination)[size],
   const char *strSource
); // C++ only
template <size_t size>
errno_t wcscat_s(
   wchar_t (&strDestination)[size],
   const wchar_t *strSource
); // C++ only
template <size_t size>
errno_t _mbscat_s(
   unsigned char (&strDestination)[size],
   const unsigned char *strSource
); // C++ only
template <size_t size>
errno_t _mbscat_s_l(
   unsigned char (&strDestination)[size],
   const unsigned char *strSource,
   _locale_t locale
); // C++ only

參數

strDestination
以 Null 終止的目的地字串緩衝區。

numberOfElements
目的地字串緩衝區的大小。

strSource
以 null 結束的來源字串緩衝區。

locale
要使用的地區設定。

傳回值

如果成功,就是零,如果失敗,則為錯誤碼。

錯誤條件

strDestination numberOfElements strSource 傳回值 strDestination 的內容。
NULL 或未終止的 任意 任意 EINVAL 未修改
任意 任意 NULL EINVAL strDestination[0] 設定為 0
任意 0 或太小 任意 ERANGE strDestination[0] 設定為 0

備註

strcat_s 函式會將 strSource 附加至 strDestination,並終止具有 Null 字元的產生字串。 strSource 的初始字元會覆寫 strDestination 的終止 Null 字元。 如果來源和目的字串重疊,則 strcat_s 的行為未定義。

第二個參數是緩衝區的總大小,而不是剩餘的大小:

char buf[16];
strcpy_s(buf, 16, "Start");
strcat_s(buf, 16, " End");               // Correct
strcat_s(buf, 16 - strlen(buf), " End"); // Incorrect

wcscat_s_mbscat_s 分別是 strcat_s 的寬字元版本和多位元組字元版本。 的引數和傳回值 wcscat_s 是寬字元字串。 的引數和傳回值 _mbscat_s 是多位元組字元字串。 除此之外,這三個函式的行為相同。

如果 strDestination 是 Null 指標,或不是以 Null 終止,或者如果 是 NULL 指標,或 strSource 目的地字串太小,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,這些函式會傳回 EINVAL,並將 errno 設為 EINVAL

具有尾碼的函式版本具有相同 _l 的行為,但使用傳入的地區設定參數,而不是目前的地區設定。 如需詳細資訊,請參閱 Locale

C++ 利用多載樣板簡化了這些函式的使用方式。多載可自動推斷緩衝區長度 (因而不須指定大小引數),也可以將不安全的舊函式自動取代成較新且安全的對應函式。 如需詳細資訊,請參閱 保護範本多載

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

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

泛型文字常式對應

TCHAR.H 常規 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tcscat_s strcat_s _mbscat_s wcscat_s

需求

常式 必要的標頭
strcat_s <string.h>
wcscat_s <string.h><wchar.h>
_mbscat_s <mbstring.h>

如需相容性詳細資訊,請參閱相容性

範例

請參閱 、 _mbscpy_swcscpy_s 中的 strcpy_s 程式碼範例。

另請參閱

字串操作
strncat, _strncat_l, wcsncat, _wcsncat_l, _mbsncat, _mbsncat_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
strspn, wcsspn, _mbsspn, _mbsspn_l