_strset_s, _strset_s_l, _wcsset_s, _wcsset_s_l, _mbsset_s, _mbsset_s_l

将字符串的字符设置为一个字符。 这些版本的 _strset_strset_l_wcsset_wcsset_l_mbsset_mbsset_l 具有安全增强功能,如 CRT 中的安全功能中所述。

重要

_mbsset_s_mbsset_s_l 无法用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数

语法

errno_t _strset_s(
   char *str,
   size_t numberOfElements,
   int c
);
errno_t _strset_s_l(
   char *str,
   size_t numberOfElements,
   int c,
   _locale_t locale
);
errno_t _wcsset_s(
   wchar_t *str,
   size_t numberOfElements,
   wchar_t c
);
errno_t *_wcsset_s_l(
   wchar_t *str,
   size_t numberOfElements,
   wchar_t c,
   _locale_t locale
);
errno_t _mbsset_s(
   unsigned char *str,
   size_t numberOfElements,
   unsigned int c
);
errno_t _mbsset_s_l(
   unsigned char *str,
   size_t numberOfElements,
   unsigned int c,
   _locale_t locale
);

参数

str
要设置的 null 终止字符串。

numberOfElements
str缓冲区的大小。

c
字符设置。

locale
要使用的区域设置。

返回值

如果成功,则为零;否则为错误代码。

这些函数将验证其参数。 如果 str 是空指针,或 numberOfElements 参数小于或等于 0,或传递的块不是以 null 终止的,则调用无效的参数处理程序,如参数验证中所述。 如果允许执行继续,则这些函数将返回 EINVAL 并将 errno 设置为 EINVAL

备注

_strset_s 函数将 str 的所有字符设置为 c(已转换为 char),终止 null 字符除外。 _wcsset_s_mbsset_s 分别是 _strset_s 的宽字符及多字节字符版本。 参数和返回值的数据类型会相应地变化。 否则这些函数具有相同行为。

输出值受区域设置的 LC_CTYPE 类别设置的影响。 有关详细信息,请参阅 setlocale。 这些不带 _l 后缀的函数的版本使用为该区域设置相关的行为的当前区域设置;带有 _l 后缀的版本相同,只不过它们使用传递的区域设置参数。 有关详细信息,请参阅 Locale

这些函数的调试库版本首先用 0xFE 填充缓冲区。 若要禁用此行为,请使用 _CrtSetDebugFillThreshold

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tcsset_s _strset_s _mbsset_s _wcsset_s
_tcsset_s_l _strset_s_l _mbsset_s_l _wcsset_s_l

要求

例程 必需的标头
_strset_s <string.h>
_strset_s_l <tchar.h>
_wcsset_s <string.h> 或 <wchar.h>
_wcsset_s_l <tchar.h>
_mbsset_s_mbsset_s_l <mbstring.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_strset_s.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   char string[] = "Fill the string with something.";
   printf( "Before: %s\n", string );
   _strset_s( string, _countof(string), '*' );
   printf( "After:  %s\n", string );
}
Before: Fill the string with something.
After:  *******************************

另请参阅

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