_mbsnbset_mbsnbset_l

将一个多字节字符串的前 n 个字节设置为指定字符。 提供这些函数的更安全版本;请参阅 _mbsnbset_s_mbsnbset_s_l

重要

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

语法

unsigned char *_mbsnbset(
   unsigned char *str,
   unsigned int c,
   size_t count
);
unsigned char *_mbsnbset_l(
   unsigned char *str,
   unsigned int c,
   size_t count,
   _locale_t locale
);

参数

str
要修改的字符串。

c
单字节或多字节字符设置。

count
要设置的字节数。

locale
要使用的区域设置。

返回值

_mbsnbset 返回指向修改后的字符串的指针。

注解

_mbsnbset_mbsnbset_l 函数最多将 str 的前 count 个字节设置为 c。 如果 count 大于 str 的长度,则会使用 str 的长度而使用 count。 如果 c 是多字节字符,且不能完全设置到由 count 指定的最后一个字节中,则用空白字符填充最后一个字节。 _mbsnbset_mbsnbset_l 不在 str 的末尾放置终止 null。

_mbsnbset_mbsnbset_l_mbsnset 类似,但它设置 count 字节而非 ccount 字符。

如果 strNULLcount 为零,则此函数将生成无效参数异常,如参数验证中所述。 如果允许继续执行,则将 errno 设置为 EINVAL 并且该函数返回 NULL中所述。 此外,如果 c 不是有效的多字节字符,则 errno 会设为 EINVAL,并改用一个空格。

输出值受区域设置的 LC_CTYPE 类别设置的影响。 有关详细信息,请参阅 setlocale。 此函数的 _mbsnbset 版本对与区域设置相关的行为使用当前区域设置,_mbsnbset_l 版本基本相同,但它使用传入的区域设置参数。 有关详细信息,请参阅 Locale

安全说明 此 API 会引发由缓冲区溢出问题带来的潜在威胁。 缓冲区溢出问题是常见的系统攻击方法,使权限的提升不能确保。 有关详细信息,请参阅避免缓冲区溢出

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

一般文本例程映射

Tchar.h 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tcsnset _strnset _mbsnbset _wcsnset
_tcsnset_l _strnset_l _mbsnbset_l _wcsnset_l

要求

例程 必需的标头
_mbsnbset <mbstring.h>
_mbsnbset_l <mbstring.h>

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

示例

// crt_mbsnbset.c
// compile with: /W3
#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( string, '*', 4 ); // C4996
   // Note; _mbsnbset is deprecated; consider _mbsnbset_s
   printf( "After:  %s\n", string );
}

输出

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

另请参阅

字符串操作
_mbsnbcat_mbsnbcat_l
_strnset_strnset_l_wcsnset_wcsnset_l_mbsnset_mbsnset_l
_strset_strset_l_wcsset_wcsset_l_mbsset_mbsset_l