Example (fwide -- Determine Stream Orientation)

This example demonstrates that fwide can be used to set the stream orientation, but cannot change the stream orientation once it has been set. The example then checks that a wide-character output operation sets the orientation as expected.

#include <stdio.h>
#include <math.h>
#include <wchar.h>
void check_orientation(FILE *stream)
{
   int rc;
   rc = fwide(stream,0);      /* check the orientation */
   if (rc<0) {
      printf("Stream has byte orientation.\n");
   } else if (rc>0)  {
      printf("Stream has wide orientation.\n");
   } else {
      printf("Stream has no orientation.\n");
   }
   return;
}
int main(void)
{
   FILE *stream;
   /* Demonstrate that fwide can be used to set the orientation,
      but cannot change it once it has been set.   */
   stream = fopen("test.dat","w");
   printf("After opening the file: ");
   check_orientation(stream);
   fwide(stream, -1);        /* Make the stream byte oriented */
   printf("After fwide(stream, -1): ");
   check_orientation(stream);
   fwide(stream,  1);        /* Try to make the stream wide oriented */
   printf("After fwide(stream,  1): ");
   check_orientation(stream);
   fclose(stream);
   printf("Close the stream\n");
   /* Check that a wide character output operation sets the orientation
      as expected.   */
   stream = fopen("test.dat","w");
   printf("After opening the file: ");
   check_orientation(stream);
   fwprintf(stream, L"pi = %.5f\n", 4* atan(1.0));
   printf("After fwprintf( ): ");
   check_orientation(stream);
   fclose(stream);
   return 0;
   /*******************************************************************
      The output should be similar to :
      After opening the file: Stream has no orientation.
      After fwide(stream, -1): Stream has byte orientation.
      After fwide(stream,  1): Stream has byte orientation.
      Close the stream
      After opening the file: Stream has no orientation.
      After fwprintf( ): Stream has wide orientation.
   *******************************************************************/
}