_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 中的安全功能中所述。

重要

_mbsnset_s_mbsnset_s_l 无法用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 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 终止的有效字符串,或者大小参数小于或等于 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

另请参阅

字符串操作
区域设置
多字节字符序列的解释
strcatwcscat_mbscat
strcmpwcscmp_mbscmp
strcpywcscpy_mbscpy
_strset_strset_l_wcsset_wcsset_l_mbsset_mbsset_l