Share via


_mbsnbset, _mbsnbset_l

將多位元組字元字串的前 n 個位元組設定為指定的字元。 這些函式有更安全的版本可供使用;請參閱 _mbsnbset_s_mbsnbset_s_l

重要

這個 API 不能用於在 Windows 執行階段中執行的應用程式。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 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 位元組,而不是 countc 字元。

如果 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