Converts a wide character string into a multibyte string and saves the parse state #include <wchar.h> size_t wcsrtombs ( char * restrict dest , const wchar_t ** restrict src , size_t len , mbstate_t * restrict state ); The wcsrtombs( ) function converts one or more wide characters from the array indirectly addressed by src into a string of multibyte characters, beginning in the parse state indicated by the state argument, and stores the results in the array of char addressed by dest. (dest may also be a null pointer, in which case wcsrtombs( ) does not actually store any output characters, and does not modify the pointer in the location addressed by src. The function therefore merely returns the number of bytes that a multibyte representation of the wide character string would occupy.) The third argument, n, specifies the maximum number of characters that can be written to the output buffer. The conversion performed on each wide character is the same as that which would be performed by the wcrtomb( ) function, updating the mbstate_t object addressed by state. Conversion ends on the first of three possible events:
Exampleint i = 0, n = 0; size_t result; wchar_t wc; char mbstring[256] = { '\0' }; wchar_t widestring[ ] = L"This is originally a string of wide characters.\n"; const wchar_t *wcsptr = widestring; mbstate_t state; printf( "The current locale is %s.\n", setlocale(LC_CTYPE, "") ); memset( &state, '\0', sizeof state ); result = wcsrtombs( mbstring, &wcsptr, 256, &state ); while ( ( n = mbtowc( &wc, &mbstring[i], MB_CUR_MAX )) != 0 ) { if ( n == -1 ) { /* Encoding error */ fputs( "Encoding error in multibyte string", stderr ); break; } printf( "%lc", (wint_t)wc ); i += n; }; See Alsowcstombs( ), wcrtomb( ), wctomb( ), mbsrtowcs( ), mbstowcs( ), mbrtowc( ), mbtowc( ) |