_TRUNCATE_TRUNCATE

指定字符串截断行为。Specifies string truncation behavior.

语法Syntax

#include <stdlib.h>  

备注Remarks

_TRUNCATE 在作为这些函数的 count 参数传递时启用截断行为:_TRUNCATE enables truncation behavior when passed as the count parameter to these functions:

strncpy_s、_strncpy_s_l、wcsncpy_s、_wcsncpy_s_l、_mbsncpy_s、_mbsncpy_s_lstrncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l

strncat_s、_strncat_s_l、wcsncat_s、_wcsncat_s_l、_mbsncat_s、_mbsncat_s_lstrncat_s, _strncat_s_l, wcsncat_s, _wcsncat_s_l, _mbsncat_s, _mbsncat_s_l

mbstowcs_s、_mbstowcs_s_lmbstowcs_s, _mbstowcs_s_l

mbsrtowcs_smbsrtowcs_s

wcstombs_s、_wcstombs_s_lwcstombs_s, _wcstombs_s_l

wcsrtombs_swcsrtombs_s

_snprintf_s、_snprintf_s_l、_snwprintf_s、_snwprintf_s_l_snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l

vsnprintf_s、_vsnprintf_s、_vsnprintf_s_l、_vsnwprintf_s、_vsnwprintf_s_lvsnprintf_s, _vsnprintf_s, _vsnprintf_s_l, _vsnwprintf_s, _vsnwprintf_s_l

如果目标缓冲区因过小而无法保存整个字符串,则这些函数的常规行为是将其视为错误情况(请参阅参数验证)。If the destination buffer is too small to hold the entire string, the normal behavior of these functions is to treat it as an error situation (see Parameter Validation). 但是,如果通过传递 _TRUNCATE 启用字符串截断,则这些函数将仅复制适当数量的字符串,使目标缓冲区以 null 结尾并成功返回。However, if string truncation is enabled by passing _TRUNCATE, these functions will copy only as much of the string as will fit, leaving the destination buffer null-terminated, and return successfully.

字符串截断将更改受影响函数的返回值。String truncation changes the return values of the affected functions. 如果未出现截断,则下列函数将返回 0;如果出现截断,则返回 STRUNCATEThe following functions return 0 if no truncation occurs, or STRUNCATE if truncation does occur:

strncpy_s、_strncpy_s_l、wcsncpy_s、_wcsncpy_s_l、_mbsncpy_s、_mbsncpy_s_lstrncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l

strncat_s、_strncat_s_l、wcsncat_s、_wcsncat_s_l、_mbsncat_s、_mbsncat_s_lstrncat_s, _strncat_s_l, wcsncat_s, _wcsncat_s_l, _mbsncat_s, _mbsncat_s_l

wcstombs_s、_wcstombs_s_lwcstombs_s, _wcstombs_s_l

mbstowcs_s、_mbstowcs_s_lmbstowcs_s, _mbstowcs_s_l

如果未出现截断,则下列函数将返回复制的字符数量;如果出现截断,则将返回 -1(与原始 snprintf 函数的行为匹配):The following functions return the number of characters copied if no truncation occurs, or -1 if truncation does occur (matching the behavior of the original snprintf functions):

_snprintf_s、_snprintf_s_l、_snwprintf_s、_snwprintf_s_l_snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l

vsnprintf_s、_vsnprintf_s、_vsnprintf_s_l、_vsnwprintf_s、_vsnwprintf_s_lvsnprintf_s, _vsnprintf_s, _vsnprintf_s_l, _vsnwprintf_s, _vsnwprintf_s_l

示例Example

// crt_truncate.c  
#include <stdlib.h>  
#include <errno.h>  

int main()  
{  
   char src[] = "1234567890";  
   char dst[5];  
   errno_t err = strncpy_s(dst, _countof(dst), src, _TRUNCATE);  
   if ( err == STRUNCATE )  
      printf( "truncation occurred!\n" );  
   printf( "'%s'\n", dst );  
}  
truncation occurred!  
'1234'  

另请参阅See Also

全局常量Global Constants