_strnset_s、_strnset_s_l、_wcsnset_s、_wcsnset_s_l、_mbsnset_s、_mbsnset_s_l_strnset_s, _strnset_s_l, _wcsnset_s, _wcsnset_s_l, _mbsnset_s, _mbsnset_s_l

将字符串的字符初始化为给定字符。Initializes characters of a string to a given character. 这些版本的 _strnset、_strnset_l、_wcsnset、_wcsnset_l、_mbsnset、_mbsnset_l 具有安全性增强功能,如 CRT 中的安全性功能中所述。These versions of _strnset, _strnset_l, _wcsnset, _wcsnset_l, _mbsnset, _mbsnset_l have security enhancements, as described in Security Features in the CRT.

重要

_mbsnset_s_mbsnset_s_l不能用于在 Windows 运行时中执行的应用程序。_mbsnset_s and _mbsnset_s_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

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
);

参数Parameters

strstr
要修改的字符串。String to be altered.

numberOfElementsnumberOfElements
大小str缓冲区。The size of the str buffer.

cc
字符设置。Character setting.

countcount
要设置的字符数。Number of characters to be set.

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

返回值Return Value

如果成功,则为零;否则为错误代码。Zero if successful, otherwise an error code.

这些函数将验证其参数。These functions validate their arguments. 如果str不是有效的以 null 结尾的字符串或大小自变量为小于或等于 0,则调用无效参数处理程序,如中所述参数验证If str is not a valid null-terminated string or the size argument is less than or equal to 0, then the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则这些函数返回错误代码和集errno该错误代码。If execution is allowed to continue, these functions return an error code and set errno to that error code. 默认错误代码是EINVAL如果更具体的值不适用。The default error code is EINVAL if a more specific value does not apply.

备注Remarks

这些函数最多,设置第一个计数字符strcThese functions set, at most, the first count characters of str to c. 如果计数大于的大小str,大小为str而不是使用计数If count is greater than the size of str, the size of str is used instead of count. 如果发生错误计数大于numberOfElements这些两个参数都大于的大小和strAn error occurs if count is greater than numberOfElements and both those parameters are greater than the size of str.

_wcsnset_s_mbsnset_s宽字符及多字节字符版本的 _strnset_s_wcsnset_s and _mbsnset_s are wide-character and multibyte-character versions of _strnset_s. 字符串自变量 _wcsnset_s是宽字符字符串; 的 _mbsnset_s是 amultibyte 字符字符串。The string argument of _wcsnset_s is a wide-character string; that of _mbsnset_s is amultibyte-character string. 否则这三个函数否则具有相同行为。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.

这些函数的调试版本首先用 0xFD 填充缓冲区。The debug versions of these functions first fill the buffer with 0xFD. 若要禁用此行为,请使用 _CrtSetDebugFillThresholdTo disable this behavior, use _CrtSetDebugFillThreshold.

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

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tcsnset_s_tcsnset_s _strnset_s_strnset_s _mbsnbset_s_mbsnbset_s _wcsnset_s_wcsnset_s
_tcsnset_s_l_tcsnset_s_l _strnset_s_l_strnset_s_l _mbsnbset_s_l_mbsnbset_s_l _wcsnset_s_l_wcsnset_s_l

要求Requirements

例程Routine 必需的标头Required header
_strnset_s_strnset_s <string.h><string.h>
_strnset_s_l_strnset_s_l <tchar.h><tchar.h>
_wcsnset_s_wcsnset_s <string.h> 或 <wchar.h><string.h> or <wchar.h>
_wcsnset_s_l_wcsnset_s_l <tchar.h><tchar.h>
_mbsnset_s_mbsnset_s_l_mbsnset_s, _mbsnset_s_l <mbstring.h><mbstring.h>

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

示例Example

// 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

请参阅See also

字符串操作String Manipulation
区域设置Locale
多字节字符序列的解释Interpretation of Multibyte-Character Sequences
strcat、wcscat、_mbscatstrcat, wcscat, _mbscat
strcmp、wcscmp、_mbscmpstrcmp, wcscmp, _mbscmp
strcpy、wcscpy、_mbscpystrcpy, wcscpy, _mbscpy
_strset、_strset_l、_wcsset、_wcsset_l、_mbsset、_mbsset_l_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l