Examples
The following example uses the fgetwc subroutine to read wide character codes from a file:
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
main()
{
wint_t retval;
FILE *fp;
wchar_t *pwcs;
(void)setlocale(LC_ALL, "");
/*
** Open a stream.
*/
fp = fopen("file", "r");
/*
** Error Handling if fopen was not successful.
*/
if(fp == NULL){
/* Error handler */
}else{
/*
** pwcs points to a wide character buffer of BUFSIZ.
*/
while((retval = fgetwc(fp)) != WEOF){
*pwcs++ = (wchar_t)retval;
/* break when buffer is full */
}
}
/* Process the wide characters in the buffer */
}
The following example uses the getwchar subroutine to read wide characters from standard input:
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
main()
{
wint_t retval;
FILE *fp;
wchar_t *pwcs;
(void)setlocale(LC_ALL, "");
index = 0;
while((retval = getwchar()) != WEOF){
/* pwcs points to a wide character buffer of BUFSIZ. */
*pwcs++ = (wchar_t)retval;
/* break on buffer full */
}
/* Process the wide characters in the buffer */
}
The following example uses the ungetwc subroutine to push a wide character onto an input stream:
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
main()
{
wint_t retval;
FILE *fp;
(void)setlocale(LC_ALL, "");
/*
** Open a stream.
*/
fp = fopen("file", "r");
/*
** Error Handling if fopen was not successful.
*/
if(fp == NULL){
/* Error handler */
else{
retval = fgetwc(fp);
if(retval != WEOF){
/*
** Peek at the character and return it to the stream.
*/
retval = ungetwc(retval, fp);
if(retval == EOF){
/* Error on ungetwc */
}
}
}
}
The following example uses the fgetws subroutine to read a file, one line at a time:
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
main()
{
FILE *fp;
wchar_t *pwcs;
(void)setlocale(LC_ALL, "");
/*
** Open a stream.
*/
fp = fopen("file", "r");
/*
** Error Handling if fopen was not successful.
*/
if(fp == NULL){
/* Error handler */
}else{
/* pwcs points to wide character buffer of BUFSIZ. */
while(fgetws(pwcs, BUFSIZ, fp) != (wchar_t *)NULL){
/*
** pwcs contains wide characters with null
** termination.
*/
}
}
}
The following example uses the fputwc subroutine to write wide characters to an output stream:
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
main()
{
int index, len;
wint_t retval;
FILE *fp;
wchar_t *pwcs;
(void)setlocale(LC_ALL, "");
/*
** Open a stream.
*/
fp = fopen("file", "w");
/*
** Error Handling if fopen was not successful.
*/
if(fp == NULL){
/* Error handler */
}else{
/* Let len indicate number of wide chars to output.
** pwcs points to a wide character buffer of BUFSIZ.
*/
for(index=0; index < len; index++){
retval = fputwc(*pwcs++, fp);
if(retval == WEOF)
break; /* write error occurred */
/* errno is set to indicate the error. */
}
}
}
The following example uses the fputws subroutine to write a wide character string to a file:
#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
main()
{
int retval;
FILE *fp;
wchar_t *pwcs;
(void)setlocale(LC_ALL, "");
/*
** Open a stream.
*/
fp = fopen("file", "w");
/*
** Error Handling if fopen was not successful.
*/
if(fp == NULL){
/* Error handler */
}else{
/*
** pwcs points to a wide character string
** to output to fp.
*/
retval = fputws(pwcs, fp);
if(retval == -1){
/* Write error occurred */
/* errno is set to indicate the error */
}
}
}