wcsrtombs()函數將寬字符轉換為多字節字符串。此函數將* src表示的寬字符串轉換為相應的多字節字符串,如果dest不為null,則存儲在dest指向的字符數組中。最多可以將len個字符寫入dest。
用法:
size_t wcsrtombs( char* dest, const wchar_t** src, size_t len, mbstate_t* ps )
參數:該函數接受四個強製性參數,如下所述:
- dest : 指向足以存儲一串最大字節的char元素數組的指針
- src : 指向要翻譯的寬字符串的指針
- len : dest數組中可用的最大字節數
- ps : 指向轉換狀態對象的指針
返回值:該函數返回兩個值,如下所示:
- 成功時,它將返回寫入dest的字節數(不包括最終的終止空字符)
- 如果發生某些錯誤,則返回-1並將errno設置為EILSEQ。
以下示例程序旨在說明上述函數:
示例1:
// C++ program to illustrate
// wcsrtombs() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize the string
const wchar_t* src = L"Geekforgeeks";
// maximum length of the dest
char dest[20];
// intial state
mbstate_t ps = mbstate_t();
// maximum length of multibyte character
int len = 12;
// function to convert wide-character
// to multibyte string
int value = wcsrtombs(dest, &src, len, &ps);
cout << "Number of multibyte characters = " << value << endl;
cout << "Multibyte characters written = " << dest << endl;
return 0;
}
輸出:
Number of multibyte characters = 12 Multibyte characters written = Geekforgeeks
示例2:
// C++ program to illustrate
// wcsrtombs() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize the string
const wchar_t* src = L"This website is the best";
// maximum length of the dest
char dest[20];
// intial state
mbstate_t ps = mbstate_t();
// maximum length of multibyte character
int len = 14;
// function to convert wide-character
// to multibyte string
int value = wcsrtombs(dest, &src, len, &ps);
cout << "Number of multibyte characters = " << value << endl;
cout << "Multibyte characters written = " << dest << endl;
return 0;
}
輸出:
Number of multibyte characters = 14 Multibyte characters written = This website i
相關用法
- C++ log()用法及代碼示例
- C++ div()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ wcsncpy()用法及代碼示例
- C++ real()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ imag()用法及代碼示例
- C++ valarray exp()用法及代碼示例
- C++ regex_iterator()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSrivastava1大神的英文原創作品 wcsrtombs() function in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。