_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 运行时执行的应用程序中使用。有关详细信息,请参见 CRT functions not supported with /ZW(CRT 函数不支持使用/ZW)。

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 后缀的版本相同,只不过它们使用通过的区域设置参数。 有关详细信息,请参阅区域设置

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

一般文本例程映射

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

.NET Framework 等效项

System::String::Replace

请参见

参考

字符串操作 (CRT)

区域设置

多字节字符序列的解释

strcat、wcscat、_mbscat

strcmp、wcscmp、_mbscmp

strcpy、wcscpy、_mbscpy

_strset、_strset_l、_wcsset、_wcsset_l、_mbsset、_mbsset_l