_mbsnbset_s、_mbsnbset_s_l_mbsnbset_s, _mbsnbset_s_l

设置的第一个n的指定字符的多字节字符字符串的字节。Sets the first n bytes of a multibyte-character string to a specified character. 这些版本的 _mbsnbset、_mbsnbset_l 具有安全性增强功能,如 CRT 中的安全功能中所述。These versions of _mbsnbset, _mbsnbset_l have security enhancements, as described in Security Features in the CRT.

重要

此 API 不能用于在 Windows 运行时中执行的应用程序。This API 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 _mbsnbset_s(
   unsigned char *str,
   size_t size,
   unsigned int c,
   size_t count
);
errno_t _mbsnbset_s_l(
   unsigned char *str,
   size_t size,
   unsigned int c,
   size_t count,
   _locale_t locale
);
template <size_t size>
errno_t _mbsnbset_s(
   unsigned char (&str)[size],
   unsigned int c,
   size_t count
); // C++ only
template <size_t size>
errno_t _mbsnbset_s_l(
   unsigned char (&str)[size],
   unsigned int c,
   size_t count,
   _locale_t locale
); // C++ only

参数Parameters

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

sizesize
字符串缓冲区的大小。The size of the string buffer.

cc
单字节或多字节字符设置。Single-byte or multibyte-character setting.

countcount
要设置的字节数。Number of bytes to be set.

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

返回值Return Value

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

备注Remarks

_Mbsnbset_s_mbsnbset_s_l函数最多,设置第一个计数字节的strc.The _mbsnbset_s and _mbsnbset_s_l functions set, at most, the first count bytes of str to c. 如果计数大于的长度str的长度str而不是使用计数If count is greater than the length of str, the length of str is used instead of count. 如果c是多字节字符,不能设置完全读入指定的最后一个字节计数,则用空白字符填充最后一个字节。If c is a multibyte character and cannot be set entirely into the last byte that's specified by count, the last byte is padded with a blank character. _mbsnbset_s_mbsnbset_s_l不放置终止 null 末尾str_mbsnbset_s and _mbsnbset_s_l do not place a terminating null at the end of str.

_mbsnbset_s_mbsnbset_s_l类似于 _mbsnset,只不过它们设置计数字节而非计数字符的c_mbsnbset_s and _mbsnbset_s_l resemble _mbsnset, except that they set count bytes rather than count characters of c.

如果strNULL计数为零,则此函数生成无效的参数异常,如中所述参数验证.If str is NULL or count is zero, this function generates an invalid parameter exception, as described in Parameter Validation. 如果允许执行继续,则errno设置为EINVAL和该函数将返回NULLIf execution is allowed to continue, errno is set to EINVAL and the function returns NULL. 此外,如果c不是有效的多字节字符, errno设置为EINVAL改为使用空格。Also, if c is not a valid multibyte character, errno is set to EINVAL and a space is used instead.

输出值受的设置LC_CTYPE的区域设置的类别设置影响; 请参阅setlocale、 _wsetlocale有关详细信息。The output value is affected by the setting of the LC_CTYPE category setting of the locale; see setlocale, _wsetlocale for more information. _Mbsnbset_s的此函数版本使用当前区域设置区域设置相关的行为; _mbsnbset_s_l版本是相同,但它改为使用的区域设置参数的在中传递。The _mbsnbset_s version of this function uses the current locale for this locale-dependent behavior; the _mbsnbset_s_l version is identical except that it instead uses the locale parameter that's passed in. 有关详细信息,请参阅 LocaleFor more information, see Locale.

在 C++ 中,这些函数的使用由模板重载简化;重载可以自动推导出缓冲区长度,从而不再需要指定大小自变量。In C++, use of these functions is simplified by template overloads; the overloads can infer buffer length automatically and thereby eliminate the need to specify a size argument. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

这些函数的调试版本首先用 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 and _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 _mbsnbset_s_l_mbsnbset_s_l _wcsnset_s_l_wcsnset_s_l

要求Requirements

例程Routine 必需的标头Required header
_mbsnbset_s_mbsnbset_s <mbstring.h><mbstring.h>
_mbsnbset_s_l_mbsnbset_s_l <mbstring.h><mbstring.h>

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.

示例Example

// crt_mbsnbset_s.c
#include <mbstring.h>
#include <stdio.h>

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

输出Output

Before: This is a test
After:  **** is a test

请参阅See also

字符串操作String Manipulation
_mbsnbcat、_mbsnbcat_l_mbsnbcat, _mbsnbcat_l
_strnset、_strnset_l、_wcsnset、_wcsnset_l、_mbsnset、_mbsnset_l_strnset, _strnset_l, _wcsnset, _wcsnset_l, _mbsnset, _mbsnset_l
_strset、_strset_l、_wcsset、_wcsset_l、_mbsset、_mbsset_l_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l