fputws() — Output a wide-character string

Standards

Standards / Extensions C or C++ Dependencies

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

both  

Format

#include <wchar.h>

int fputws(const wchar_t * __restrict__wcs, FILE * __restrict__stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <wchar.h>

int fputws_unlocked(const wchar_t * __restrict__wcs, FILE * __restrict__stream);

General description

Converts the wide-character string pointed to by wcs to a multibyte character string and writes it to the stream pointed to by stream, as a multibyte character string. The terminating NULL byte is not written.

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

fputws() 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.

fputws_unlocked() is functionally equivalent to fputws() 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.

Returned value

If successful, fputws() returns a nonnegative value.

If a stream error occurs, fputws() returns -1 and the error indicator for the stream is set.

If an encoding error occurs, fputws() returns -1 and the value of the macro EILSEQ is stored in errno. An encoding error is one that occurs when converting a wide character to a multibyte character.

Example

CELEBF37
⁄* CELEBF37 *⁄                                   
#include <errno.h>                                                              
#include <stdio.h>                                                              
#include <stdlib.h>                                                             
#include <wchar.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   FILE    *stream;                                                             
   wchar_t *wcs = L"This test string should not return -1";                     
   int     rc;                                                                  
                                                                                
   if ((stream = fopen("myfile.dat", "w")) == NULL) {                           
      printf("Unable to open file.\n");                                         
      exit(1);                                                                  
   }                                                                            
                                                                                
   errno = 0;                                                                   
   rc = fputws(wcs, stream);                                                    
                                                                                
   if (rc == EOF) {                                                             
      printf("Unable to complete fputws() function.\n");                        
      if (errno == EILSEQ)                                                      
         printf("An invalid wide character was encountered.\n");                
      exit(1);                                                                  
   }                                                                            
                                                                                
   fclose(stream);                                                              
}                                                                               

Related information