📜  C / C++中的wcsrtombs()函数

📅  最后修改于: 2021-05-25 19:05:41             🧑  作者: Mango

wcsrtombs()函数将宽字符转换为多字节字符串。此函数由* SRC表示的宽转换为对应的多字节字符的字符串和存储在字符数组指向DEST如果DEST不是null。最多可将len个字符写入dest

句法:

参数:该函数接受四个强制性参数,如下所述:

  • dest:指向足以存储字符串最大字节的char元素数组的指针
  • src:指向要翻译的宽字符串的指针
  • len: dest数组中可用的最大字节数
  • ps:指向转换状态对象的指针

返回值:函数返回两个值,如下所示:

  • 成功时,它返回写入dest的字节数(不包括最终的终止null字符)
  • 如果发生某些错误,则返回-1,并将errno设置为EILSEQ

下面的程序说明了上述函数:
程序1:

// C++ program to illustrate
// wcsrtombs() function
#include 
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 
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的C基础课程》。