┌──────────────────────────────────────────────────────────────────────┐ │A stream can be accessed either by byte-based or by wchar_t based. │ │From an Internationalization's view point, wide-based access is │ │recommended. │ └──────────────────────────────────────────────────────────────────────┘
In the IBM Visual Age C++ for OS/2, all input and output are mapped into logical data streams, either in text or in binary. An application can access a text stream with multibyte based operations or wide character based operations. By using wide-based operations, the application can read and write wide strings in the same manner as multibyte strings. Moreover, wide-character will make the application encoding and code page independent.
Coresspondence between byte-based and wide-based stream I/O functions. Equivalent stdio I/O functions are available respectively.
┌────────────────────┬────────────────────┬─────────────────────────┐ │byte-based │wide-based │Descriptions │ ├────────────────────┼────────────────────┼─────────────────────────┤ │getc/getchar │getwc/getwchar │Read a (wide) character │ │ │ │from the stdio. │ ├────────────────────┼────────────────────┼─────────────────────────┤ │fgetc │fgetwc │Read a (wide) character │ │ │ │from a specified input │ │ │ │stream. │ ├────────────────────┼────────────────────┼─────────────────────────┤ │fgets │fgetws │Read a (wide) string from│ │ │ │a specified input stream.│ ├────────────────────┼────────────────────┼─────────────────────────┤ │putc/putchar │putwc/putwchar │Prints a (wide) character│ │ │ │to the stdio. │ ├────────────────────┼────────────────────┼─────────────────────────┤ │fputc │fputwc │Prints a (wide) character│ │ │ │to a specified output │ │ │ │stream. │ ├────────────────────┼────────────────────┼─────────────────────────┤ │fputs │fputws │Prints a (wide) string to│ │ │ │a specified output │ │ │ │stream. │ ├────────────────────┼────────────────────┼─────────────────────────┤ │ungetc │ungetwc │Pushes a (wide) character│ │ │ │back to a specified input│ │ │ │stream. │ └────────────────────┴────────────────────┴─────────────────────────┘In addition, %lc and %ls specifiers of the formatted I/O functions such as fprintf() and scanf() can be used to perform wide-based stream I/O.
... #include <stdio.h> #include <wchar.h> ... char* readProdFile() { FILE* fh; wchar_t awcBuf[PROD_REC_MAXLEN+2]; /* +2 for '\n' & '\0' */ ... if( (fh = fopen( pszFnameProd, "r" )) == NULL ) { logError( ERR_FILE_OPEN, MPFROMP(pszFnameProd) ); return NULL; } while( fgetws( awcBuf, PROD_REC_MAXLEN+2, fh ) != NULL ) { ... } }
Note: Once you apply the wide-I/O functions to a stream, using non-wide-character functions on the same stream results in an unexpected error in the ungetwc() function.