The wcsrtombs() function is defined in <cwchar> header file.
size_t wcsrtombs( char* dest, const wchar_t** src, size_t len, mbstate_t* ps );
The wcsrtombs() 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.
The conversion process is similar to calling wcrtomb() repeatedly. The conversion stops if:
- A wide null character was converted and stored. In this case, src is set to null and ps represents initial shift state.
- An invalid wide character was encountered. In this case, src is set to point the beginning of the first unconverted wide character.
- len bytes has been stored in dest. In this case, src is set to point the beginning of the first unconverted wide character.
- >dest: Pointer to the character array where the converted multibyte character is stored.
- src: Pointer to pointer to the first wide character to convert.
- len: Maximum number of bytes available in dest array.
- ps: Pointer to the conversion state object.
wcsrtombs() Return value
- On success, the wcsrtombs() function returns the number of multibyte characters written to dest excluding the terminating wide null character but including shift sequences.
If dest is a null pointer, it returns the number of wide characters that would have been written excluding the terminating null character.
- On conversion error, -1 is returned and errno is set to EILSEQ.
Example: How wcsrtombs() function works?
using namespace std;
const wchar_t* wstr = L"\u0763\u0757\u077f\u075f";
mbstate_t ps = mbstate_t();
int len = 10;
retVal = wcsrtombs(str, &wstr, len, &ps);
cout << "Number of multibyte characters written (excluding \"\\0\") = " << retVal << endl;
cout << "Multibyte character = " << str << endl;
When you run the program, the output will be:
Number of multibyte characters written (excluding "\0") = 8
Multibyte character = ݣݗݿݟ