Share via


_strnset_s, _strnset_s_l, _wcsnset_s, _wcsnset_s_l, _mbsnset_s, _mbsnset_s_l

將字串字元初始化為指定的字元。 這些版本的 _strnset_strnset_l_wcsnset_wcsnset_l_mbsnset 具有 _mbsnset_l 安全性增強功能,如 CRT 的安全性功能中所述

重要

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

語法

errno_t _strnset_s(
   char *str,
   size_t numberOfElements,
   int c,
   size_t count
);
errno_t _strnset_s_l(
   char *str,
   size_t numberOfElements,
   int c,
   size_t count,
   _locale_t locale
);
errno_t _wcsnset_s(
   wchar_t *str,
   size_t numberOfElements,
   wchar_t c,
   size_t count
);
errno_t _wcsnset_s_l(
   wchar_t *str,
   size_t numberOfElements,
   wchar_t c,
   size_t count,
   _locale_t locale
);
errno_t _mbsnset_s(
   unsigned char *str,
   size_t numberOfElements,
   unsigned int c,
   size_t count
);
errno_t _mbsnset_s_l(
   unsigned char *str,
   size_t numberOfElements,
   unsigned int c,
   size_t count,
   _locale_t locale
);

參數

str
待變更字串。

numberOfElements
str 緩衝區的大小。

c
字元設定。

count
要設定的字元數。

locale
要使用的地區設定。

傳回值

如果成功則為零,否則為錯誤碼。

這些函式會驗證它們的引數。 如果 str 不是有效的 Null 終止字串,或 size 引數小於或等於 0,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,這些函式會傳回錯誤碼並將 errno 設為該錯誤碼。 如果不適用更特定的值,則預設錯誤碼為 EINVAL

備註

這些函式最多會將 str 的前 count 個字元附加至 c。 如果 count 大於 str 的大小,則會使用 str 的大小,而非 count。 如果 count 大於 numberOfElements 且這兩個參數都大於 str 的大小,會發生錯誤。

_wcsnset_s_mbsnset_s 分別是 _strnset_s 的寬字元版本和多位元組字元版本。 的字串引數 _wcsnset_s 是寬字元字串,其 _mbsnset_s 為多位元組字元字串。 除此之外,這三個函式的行為相同。

輸出值會受到 LC_CTYPE 地區設定之類別設定的影響。 如需詳細資訊,請參閱setlocale。 這些沒有 _l 後置字元的函式版本,會針對此與地區設定相關的行為使用目前的地區設定;具有 _l 後置字元的版本也一樣,只不過它們會改用傳遞的地區設定參數。 如需詳細資訊,請參閱 Locale

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

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

泛型文字常式對應

TCHAR.H 常式 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tcsnset_s _strnset_s _mbsnbset_s _wcsnset_s
_tcsnset_s_l _strnset_s_l _mbsnbset_s_l _wcsnset_s_l

需求

常式 必要的標頭
_strnset_s <string.h>
_strnset_s_l <tchar.h>
_wcsnset_s <string.h > 或 < wchar.h>
_wcsnset_s_l <tchar.h>
_mbsnset_s, _mbsnset_s_l <mbstring.h>

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

範例

// crt_strnset_s.c
#include <string.h>
#include <stdio.h>

int main( void )
{
   char string[15] = "This is a test";
   /* Set not more than 4 characters of string to be *'s */
   printf( "Before: %s\n", string );
   _strnset_s( string, sizeof(string), '*', 4 );
   printf( "After:  %s\n", string );
}
Before: This is a test
After:  **** is a test

另請參閱

字串操作
地區設定
多位元組字元序列的解譯
strcat, wcscat, _mbscat
strcmp, wcscmp, _mbscmp
strcpy, wcscpy, _mbscpy
_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l