Syntax
#include <stdio.h> #include <wchar.h> wint_t putwc(wchar_t wc, FILE *stream);Description
putwc converts the wide character wc to a multibyte character, and writes it to the stream at the current position. It also advances the file position indicator for the stream appropriately.
putwc function is equivalent to fputwc except that, if it is implemented as a macro, putwc can evaluate stream more than once. Therefore, the stream argument to putwc should not be an expression with side effects.
The behavior of putwc is affected by the LC_CTYPE category of the current locale.
After calling putwc, flush the buffer or reposition the stream pointer before calling a read function for the stream, unless EOF has been reached. After a read operation on the stream, flush the buffer or reposition the stream pointer before calling putwc.
putwc returns the wide character written. If a write error occurs, putwc sets the error indicator for the stream and returns WEOF. If an encoding error occurs when a wide character is converted to a multibyte character, putwc sets errno to EILSEQ and returns WEOF.
The following example uses putwc to convert the wide characters in wcs to multibyte characters and write them to the file putwc.out.
#include <stdio.h> #include <wchar.h> #include <stdlib.h> #include <errno.h> int main(void) { FILE *stream; wchar_t *wcs = L"A character string."; int i; if (NULL == (stream = fopen("putwc.out", "w"))) { printf("Unable to open: \"putwc.out\".\n"); exit(EXIT_FAILURE); } for (i = 0; wcs[i] != L'\0'; i++) { errno = 0; if (WEOF == putwc(wcs[i], stream)) { printf("Unable to putwc() the wide character.\n" "wcs[%d] = 0x%lx\n", i, wcs[i]); if (EILSEQ == errno) printf("An invalid wide character was encountered.\n"); exit(EXIT_FAILURE); } } fclose(stream); return 0; /**************************************************************************** The output file putwc.out should contain : A character string. ****************************************************************************/ }Related Information