strcat_s, wcscat_s, _mbscat_s, _mbscat_s_l

追加字符串。 这些版本的 strcatwcscat_mbscat 具有安全增强功能,如 CRT 中的安全功能中所述。

重要

_mbscat_s_mbscat_s_l 无法用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 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 或未终止 任意 any EINVAL 未修改
any any NULL EINVAL strDestination[0]:设置为 0
any 0 或过小 any ERANGE strDestination[0]:设置为 0

备注

strcat_s 函数将 strSource 追加到 strDestination,并使用空字符终止结果字符串。 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 终止,或如果 strSourceNULL 指针,亦或是如果目标字符串过小,则调用无效的参数处理程序,如参数验证中所述。 如果允许执行继续,则这些函数将返回 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>

有关兼容性的详细信息,请参阅 兼容性

示例

有关代码示例,请参阅strcpy_swcscpy_s_mbscpy_s

另请参阅

字符串操作
strncat_strncat_lwcsncat_wcsncat_l_mbsncat_mbsncat_l
strncmpwcsncmp_mbsncmp_mbsncmp_l
strncpy_strncpy_lwcsncpy_wcsncpy_l_mbsncpy_mbsncpy_l
_strnicmp_wcsnicmp_mbsnicmp_strnicmp_l_wcsnicmp_l_mbsnicmp_l
strrchrwcsrchr_mbsrchr_mbsrchr_l
strspnwcsspn_mbsspn_mbsspn_l