_TRUNCATE
指定字串截斷行為。
#include <stdlib.h>
備註
_TRUNCATE啟用截斷行為時傳遞做為count給這些函數的參數:
strncpy_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_l
_snprintf_s、 _snprintf_s_l、 _snwprintf_s、 _snwprintf_s_l
vsnprintf_s、 _vsnprintf_s、 _vsnprintf_s_l、 _vsnwprintf_s、 _vsnwprintf_s_l
如果目的緩衝區太小,無法容納整個字串,這些函式的正常的行為是要視為錯誤狀況 (請參閱參數驗證)。 不過,如果字串截斷啟用藉由傳遞_TRUNCATE,這些函式會將複製只盡可能填滿,保留 null 結尾的目的緩衝區的字串,並傳回成功。
字串截斷變更受影響的函式的傳回值。 下列函數會傳回 0 無截斷發生,或STRUNCATE如果截斷並不會發生:
strncpy_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_l
如果發生截斷 (比對原始的 snprintf 函式的行為),下列函數會傳回發生無截斷時,所複製的字元,則為-1 的數字:
_snprintf_s、 _snprintf_s_l、 _snwprintf_s、 _snwprintf_s_l
vsnprintf_s、 _vsnprintf_s、 _vsnprintf_s_l、 _vsnwprintf_s、 _vsnwprintf_s_l
範例
// 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 );
}