fputwc() — Output a wide-character

Standards

Standards / Extensions C or C++ Dependencies

XPG4
XPG4.2
Single UNIX Specification, Version 3
Language Environment

both  

Format

Non-XPG4:
#include <stdio.h>
#include <wchar.h>

wint_t fputwc(wchar_t wc, FILE *stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <wchar.h>

wint_t fputwc_unlocked(wchar_t wc, FILE *stream);
XPG4:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <wchar.h>

wint_t fputwc(wint_t wc, FILE *stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <wchar.h>

wint_t fputwc_unlocked(wchar_t wc, FILE *stream);
XPG4 and MSE:
#define _XOPEN_SOURCE
#define _MSE_PROTOS
#include <stdio.h>
#include <wchar.h>

wint_t fputwc(wchar_t wc, FILE *stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <wchar.h>

wint_t fputwc_unlocked(wchar_t wc, FILE *stream);

General description

Converts the wide character specified by wc to a multibyte character and writes it to the output stream pointed to by stream, at the position indicated by the associated file position indicator for the stream (if defined), and advances the indicator appropriately. If the file cannot support positioning requests or if the stream was opened with append mode, the character is appended to the output stream.

The behavior of this wide-character function is affected by the LC_CTYPE category of the current locale. Using non-wide-character functions with fputwc() results in undefined behavior.

fputwc() has the same restriction as any write operation for a read immediately following a write or a write immediately following a read. Between a write and a subsequent read, there must be an intervening flush or reposition. Between a read and a subsequent write, there must also be an intervening flush or reposition unless an EOF has been reached.

fputwc_unlocked() is functionally equivalent to fputwc() with the exception that it is not thread-safe. This function can safely be used in a multithreaded application if and only if it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.

Special behavior for XPG4: If you define any feature test macro specifying XPG4 behavior before the statement in your program source file to include the wchar header, then the compiler assumes that your program is using the XPG4 variety of the fputwc() function, unless you also define the _MSE_PROTOS feature test macro. Please see Table 1 for a list of XPG4 and other feature test macros.

The prototype for the XPG4 variety of the fputwc() function is:
wint_t fputwc(wint_t wc, FILE *stream);

The difference between this variety and the MSE variety of the fputwc() function is that the first parameter has type wint_t rather than type wchar_t.

Returned value

If successful, fputwc() returns the wide character written.

If a write error occurs, the error indicator for the stream is set and WEOF is returned. If an encoding error occurs during conversion from wide character to a multibyte character, the value of the macro EILSEQ is stored in errno and WEOF is returned.

Example

CELEBF36
⁄* CELEBF36 *⁄                                                                                                                 
#include <stdio.h>                                                              
#include <stdlib.h>                                                             
#include <wchar.h>                                                              
#include <errno.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   FILE    *stream;                                                             
   wchar_t *wcs = L"This test string should not cause a WEOF condition";        
   int     i;                                                                   
   int     rc;                                                                  
                                                                                
   if ((stream = fopen("myfile.dat", "w")) == NULL) {                           
      printf("Unable to open file.\n");                                         
      exit(1);                                                                  
   }                                                                            
                                                                                
   for (i=0; wcs[i] != L'\0'; i++) {                                            
      errno = 0;                                                                
      if ((rc = fputwc(wcs[i], stream)) == WEOF) {                              
         printf("Unable to fputwc() the wide character.\n");                    
         printf("wcs[%d] = 0x%lx\n", i, wcs[i]);                                
         if (errno == EILSEQ)                                                   
            printf("An invalid wide character was encountered.\n");             
         exit(1);                                                               
      }                                                                         
   }                                                                            
                                                                                
   fclose(stream);                                                              
}                                                                               

Related information