wcsrtombs() function in C/C++
Last Updated :
08 Jun, 2021
The wcsrtombs() function converts wide character to multibyte string. This function converts the wide character string represented by *src to corresponding multibyte character string and is stored in the character array pointed to by dest if dest is not null. A maximum of len characters are written to dest.
Syntax:
size_t wcsrtombs( char* dest, const wchar_t** src, size_t len, mbstate_t* ps )
Parameters: The function accepts four mandatory parameters which are described below:
- dest : Pointer to an array of char elements long enough to store a string of max bytes
- src : Pointer to a wide string to be translated
- len : Maximum number of bytes available in dest array
- ps : Pointer to the conversion state object
Return value : The function returns two value as below:
- On success it returns the number of bytes written to dest(not including the eventual terminating null character)
- If some error occurs then -1 is returned and errno is set to EILSEQ.
Below programs illustrate the above function:
Program 1:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
const wchar_t * src = L "Geekforgeeks" ;
char dest[20];
mbstate_t ps = mbstate_t ();
int len = 12;
int value = wcsrtombs(dest, &src, len, &ps);
cout << "Number of multibyte characters = " << value << endl;
cout << "Multibyte characters written = " << dest << endl;
return 0;
}
|
Output:
Number of multibyte characters = 12
Multibyte characters written = Geekforgeeks
Program 2:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
const wchar_t * src = L "This website is the best" ;
char dest[20];
mbstate_t ps = mbstate_t ();
int len = 14;
int value = wcsrtombs(dest, &src, len, &ps);
cout << "Number of multibyte characters = " << value << endl;
cout << "Multibyte characters written = " << dest << endl;
return 0;
}
|
Output:
Number of multibyte characters = 14
Multibyte characters written = This website i
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...