home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / i18nv104.zip / SAMPLE / IO / IO.C < prev    next >
C/C++ Source or Header  |  1996-02-13  |  5KB  |  106 lines

  1. /*****************************************************************************/
  2. /***     Include files                                                     ***/
  3. /*****************************************************************************/
  4.  
  5. #include <stdio.h>                      /* Standard IO functions.            */
  6. #include <locale.h>
  7. #include <wchar.h>                      /* XPG/4 library.                    */
  8.  
  9. /*****************************************************************************/
  10. /***     Global defines                                                    ***/
  11. /*****************************************************************************/
  12.  
  13. #define NOT_END_OF_FILE 0
  14.  
  15. /*****************************************************************************/
  16. /***     Global variables                                                  ***/
  17. /*****************************************************************************/
  18.  
  19. FILE *test_file;                        /* File handle for input and output. */
  20.  
  21. /*****************************************************************************/
  22. /***     Perform wchar_t IO tests.                                         ***/
  23. /*****************************************************************************/
  24.  
  25. void do_w_io_tests(void)
  26. {
  27.   int count;
  28.   wchar_t a_w_char;
  29.   char    a_string[400];
  30.   wchar_t a_w_string[200];
  31.  
  32.                                         /* Convert a string into a wchar_t   */
  33.                                         /*  string.                          */
  34.   mbstowcs(a_w_string, "A wchar_t string!\n", sizeof(a_w_string));
  35.  
  36.                                         /* Open up output file (for update). */
  37.                                         /* Print a header.                   */
  38.   test_file = fopen("OUTPUT", "w+");
  39.   printf("\nDemonstrate the 'put' functions for wchar_t:\nString: ");
  40.  
  41.                                         /* This loop iterates through the    */
  42.                                         /*  characters of a wchar_t string,  */
  43.                                         /*  and puts each character out to   */
  44.                                         /*  standard output, and to a file.  */
  45.   for (count = 0; count < wcslen(a_w_string); count++)
  46.   {
  47.       a_w_char = a_w_string[count];
  48.       putwchar(a_w_char);
  49.       putwc(a_w_char, test_file);       /* Same as fputwc function.          */
  50.   }
  51.  
  52.                                         /* Put a whole string of wchar_t to  */
  53.                                         /*  the output file.                 */
  54.   mbstowcs(a_w_string, 
  55.        "Another wchar_t string to put in the file!\n", sizeof(a_w_string));
  56.   fputws(a_w_string, test_file);
  57.  
  58.                                         /* Print a header.                   */
  59.                                         /* Go back to start of test file.    */
  60.                                         /* Print out the first wchar_t string*/
  61.                                         /*  in the file.                     */
  62.   printf("\nDemonstrate the 'get' functions for wchar_t:\n");
  63.   rewind(test_file);
  64.   printf("Reading the first line from the file: %S\n",
  65.          fgetws(a_w_string, 100, test_file));
  66.  
  67.                                         /* Put a character back on the input */
  68.                                         /*  stream (even though it wasn't    */
  69.                                         /*  read from the stream.            */
  70.                                         /* Print a header.                   */
  71.                                         /* Loop 10 times...                  */
  72.                                         /*  Get a wchar_t from the stream.   */
  73.                                         /*  Put it out to std input.         */
  74.   mbtowc(&a_w_char, "?", MB_CUR_MAX);
  75.   ungetwc(a_w_char, test_file);
  76.   printf("Next 10 wchar_t from file: ");
  77.   for (count = 0; count < 10; count++)
  78.   {
  79.     a_w_char = getwc(test_file);        /* Same as fgetwc function.          */
  80.     putwchar(a_w_char);
  81.   }
  82.  
  83.                                         /* Print a header.                   */
  84.                                         /* Get a wchar_t from std input.     */
  85.                                         /* Print out results.                */
  86.   printf("\n\nPlease enter a character, and then press <ENTER>:\n");
  87.   a_w_char = getwchar();
  88.   printf("The character entered was: %C\n", a_w_char);
  89.  
  90.                                         /* Close test output file.           */
  91.   fclose(test_file);
  92. }
  93.  
  94. /*****************************************************************************/
  95. /***     Main program                                                      ***/
  96. /*****************************************************************************/
  97.  
  98. void main(void)
  99. {
  100.                                         /* Demonstrate the wchar_t IO        */
  101.                                         /*  functions.                       */
  102.   setlocale(LC_ALL, "");
  103.   do_w_io_tests();
  104.  
  105. }
  106.