strncat、_strncat_l、wcsncat、_wcsncat_l、_mbsncat、_mbsncat_lstrncat, _strncat_l, wcsncat, _wcsncat_l, _mbsncat, _mbsncat_l

向字符串追加字符。Appends characters of a string. 这些函数的更安全版本已经发布,请参阅 strncat_s、_strncat_s_l、wcsncat_s、_wcsncat_s_l、_mbsncat_s、_mbsncat_s_lMore secure versions of these functions are available, see strncat_s, _strncat_s_l, wcsncat_s, _wcsncat_s_l, _mbsncat_s, _mbsncat_s_l .

重要

_mbsncat_mbsncat_l不能在 Windows 运行时中执行的应用程序中使用。_mbsncat and _mbsncat_l cannot be used in applications that execute in the Windows Runtime. 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数For more information, see CRT functions not supported in Universal Windows Platform apps.

语法Syntax

char *strncat(
   char *strDest,
   const char *strSource,
   size_t count
);
wchar_t *wcsncat(
   wchar_t *strDest,
   const wchar_t *strSource,
   size_t count
);
unsigned char *_mbsncat(
   unsigned char *strDest,
   const unsigned char *strSource,
   size_t count
);
unsigned char *_mbsncat_l(
   unsigned char *strDest,
   const unsigned char *strSource,
   size_t count,
   _locale_t locale
);
template <size_t size>
char *strncat(
   char (&strDest)[size],
   const char *strSource,
   size_t count
); // C++ only
template <size_t size>
wchar_t *wcsncat(
   wchar_t (&strDest)[size],
   const wchar_t *strSource,
   size_t count
); // C++ only
template <size_t size>
unsigned char *_mbsncat(
   unsigned char (&strDest)[size],
   const unsigned char *strSource,
   size_t count
); // C++ only
template <size_t size>
unsigned char *_mbsncat_l(
   unsigned char (&strDest)[size],
   const unsigned char *strSource,
   size_t count,
   _locale_t locale
); // C++ only

参数Parameters

strDeststrDest
null 终止的目标字符串。Null-terminated destination string.

strSourcestrSource
null 终止的源字符串。Null-terminated source string.

countcount
要追加的字符数。Number of characters to append.

localelocale
要使用的区域设置。Locale to use.

返回值Return Value

返回一个指向目标字符串的指针。Returns a pointer to the destination string. 没有保留任何返回值以指示错误。No return value is reserved to indicate an error.

备注Remarks

Strncat函数至多,追加第一个计数字符strSourcestrDestThe strncat function appends, at most, the first count characters of strSource to strDest. 初始字符strSource覆盖的终止 null 字符strDestThe initial character of strSource overwrites the terminating null character of strDest. 如果中出现 null 字符strSource之前计数追加字符, strncat追加中的所有字符strSource,直到 null 字符。If a null character appears in strSource before count characters are appended, strncat appends all characters from strSource, up to the null character. 如果计数大于的长度strSource,时长strSource用来代替计数If count is greater than the length of strSource, the length of strSource is used in place of count. 在所有情况下,结果字符串以 null 字符终止。The all cases, the resulting string is terminated with a null character. 如果复制出现在重叠的字符串之间,则该行为不确定。If copying takes place between strings that overlap, the behavior is undefined.

重要

strncat不会有足够的空间中检查strDest; 因此,是可能会造成缓冲区溢出。strncat does not check for sufficient space in strDest; it is therefore a potential cause of buffer overruns. 请记住计数限制数字符追加; 它不是限制的大小strDestKeep in mind that count limits the number of characters appended; it is not a limit on the size of strDest. 请参见下面的示例。See the example below. 有关详细信息,请参阅 避免缓冲区溢出For more information, see Avoiding Buffer Overruns.

wcsncat_mbsncat宽字符及多字节字符版本的strncatwcsncat and _mbsncat are wide-character and multibyte-character versions of strncat. 字符串参数和返回值wcsncat是宽字符字符串; _mbsncat是多字节字符字符串。The string arguments and return value of wcsncat are wide-character strings; those of _mbsncat are multibyte-character strings. 否则这三个函数否则具有相同行为。These three functions behave identically otherwise.

输出值受区域设置的 LC_CTYPE 类别设置影响;有关详细信息,请参阅 setlocaleThe output value is affected by the setting of the LC_CTYPE category setting of the locale; see setlocale for more information. 这些不带 _l 后缀的函数版本使用此区域设置相关的行为的当前区域设置;带有 _l 后缀的版本相同,只不过它们使用传递的区域设置参数。The versions of these functions without the _l suffix use the current locale for this locale-dependent behavior; the versions with the _l suffix are identical except that they use the locale parameter passed in instead. 有关详细信息,请参阅 LocaleFor more information, see Locale.

在 C++ 中,这些函数具有模板重载。In C++, these functions have template overloads. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tcsncat_tcsncat strncatstrncat _mbsnbcat_mbsnbcat wcsncatwcsncat
_tcsncat_l_tcsncat_l _strncat_l_strncat_l _mbsnbcat_l_mbsnbcat_l _wcsncat_l_wcsncat_l

备注

_strncat_l_wcsncat_l没有区域设置相关性,并且不应直接调用。_strncat_l and _wcsncat_l have no locale dependence and are not meant to be called directly. 它们提供供内部使用 _tcsncat_lThey are provided for internal use by _tcsncat_l.

要求Requirements

例程所返回的值Routine 必需的标头Required header
strncatstrncat <string.h><string.h>
wcsncatwcsncat <string.h> 或 <wchar.h><string.h> or <wchar.h>
_mbsncat_mbsncat <mbstring.h><mbstring.h>
_mbsncat_l_mbsncat_l <mbstring.h><mbstring.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// crt_strncat.c
// Use strcat and strncat to append to a string.
#include <stdlib.h>

#define MAXSTRINGLEN 39

char string[MAXSTRINGLEN+1];
// or char *string = malloc(MAXSTRINGLEN+1);

void BadAppend( char suffix[], int n )
{
   strncat( string, suffix, n );
}

void GoodAppend( char suffix[], size_t n )
{
   strncat( string, suffix, __min( n, MAXSTRINGLEN-strlen(string)) );
}

int main( void )
{
   string[0] = '\0';
   printf( "string can hold up to %d characters\n", MAXSTRINGLEN );

   strcpy( string, "This is the initial string!" );
   // concatenate up to 20 characters...
   BadAppend( "Extra text to add to the string...", 20 );
   printf( "After BadAppend :  %s (%d chars)\n", string, strlen(string) );

   strcpy( string, "This is the initial string!" );
   // concatenate up to 20 characters...
   GoodAppend( "Extra text to add to the string...", 20 );
   printf( "After GoodAppend:  %s (%d chars)\n", string, strlen(string) );
}

输出Output

string can hold up to 39 characters
After BadAppend :  This is the initial string!Extra text to add to (47 chars)
After GoodAppend:  This is the initial string!Extra text t (39 chars)

请注意, BadAppend导致缓冲区溢出。Note that BadAppend caused a buffer overrun.

请参阅See also

字符串操作String Manipulation
_mbsnbcat、_mbsnbcat_l_mbsnbcat, _mbsnbcat_l
strcat、wcscat、_mbscatstrcat, wcscat, _mbscat
strcmp、wcscmp、_mbscmpstrcmp, wcscmp, _mbscmp
strcpy、wcscpy、_mbscpystrcpy, wcscpy, _mbscpy
strncmp、wcsncmp、_mbsncmp、_mbsncmp_lstrncmp, wcsncmp, _mbsncmp, _mbsncmp_l
strncpy、_strncpy_l、wcsncpy、_wcsncpy_l、_mbsncpy、_mbsncpy_lstrncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l
_strnicmp、_wcsnicmp、_mbsnicmp、_strnicmp_l、_wcsnicmp_l、_mbsnicmp_l_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr、wcsrchr、_mbsrchr、_mbsrchr_lstrrchr, wcsrchr, _mbsrchr, _mbsrchr_l
_strset、_strset_l、_wcsset、_wcsset_l、_mbsset、_mbsset_l_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l
strspn、wcsspn、_mbsspn、_mbsspn_lstrspn, wcsspn, _mbsspn, _mbsspn_l
区域设置Locale
多字节字符序列的解释Interpretation of Multibyte-Character Sequences