C++ Standard Library C++ STL Library

C++ <cwchar> - fwide() Function



The C++ <cwchar> fwide() function is used to determine the orientation of stream. If the orientation of the stream is not yet established, the function attempts to make stream oriented depending on the value of mode.

  • If mode > 0, it attempts to make stream wide-oriented.
  • If mode < 0, it attempts to make stream byte-oriented.
  • If mode == 0, it only queries the current orientation of the stream.

If the orientation of the stream has already been decided (by executing output or by an earlier call to fwide()), the function do nothing.

Syntax

int fwide (FILE* stream, int mode);        

Parameters

stream Specify a pointer to a FILE object that specifies a stream.
mode Specify integer value greater than zero to set the stream wide, less than zero to set the stream narrow, or zero to query only.

Return Value

Returns value depending on the stream orientation after the call.

  • A integer value greater than 0, if the stream is wide-oriented.
  • A integer value less than 0, if the stream is byte-oriented.
  • A value equal to 0, if the stream has no orientation.

Example:

The example below shows the usage of fwide() function.

#include <cwchar>
#include <cstdio>

void show_orientation(int n) {
  if(n == 0) printf("No stream orientation.\n");
  else if (n < 0) printf("Stream is byte-oriented.\n");
  else printf("Stream is wide-oriented.\n");
}

int main (){
  //open the file in write mode
  FILE *pFile = fopen("test.txt", "w");

  if (pFile) {
    //A newly opened stream has no orientation.
    show_orientation(fwide(pFile, 0));
 
    //Establish wide orientation.
    show_orientation(fwide(pFile, 1));

    //close the file
    fclose(pFile);
  }

  return 0;
}

The output of the above code will be:

No stream orientation.
Stream is wide-oriented.

❮ C++ <cwchar> Library

5