C++ fwide()

The fwide() function in C++ either attempts to set the orientation or queries the current orientation of a given file stream.

The fwide() function is defined in <cwchar> header file.

fwide() prototype

int fwide( FILE* stream, int mode );

Based on the value of mode, it is decided what the fwide function does.

  • If mode > 0, this function attempts to make stream wide-oriented.
  • If mode < 0, this function attempts to make stream byte-oriented.
  • If mode == 0, this function 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, this function does nothing.

fwide() Parameters

  • stream: Pointer to the file stream to set or query the orientation.
  • mode: An integer value that determines whether to set or query the orientation of the stream.

fwide() Return value

The fwide() function returns:

  • A positive integer if the stream is wide-oriented.
  • A negative integer if the stream is byte-oriented.
  • Zero if the stream has no orientation.

Example: How fwide() function works?

#include <cwchar>
#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	FILE *fp;
	int retVal;

	fp = fopen("file.txt","r");
	retVal = fwide(fp,0);
	if (retVal == 0)
		cout << "Stream has no orientation" << endl;
	else if (retVal > 0)
		cout << "Stream is wide-oriented" << endl;
	else
		cout << "Stream is byte-oriented" << endl;

	/* wide oriented stream */
	cout << "Setting stream to wide-orientation" << endl;
	retVal = fwide(fp,1);
	if (retVal == 0)
		cout << "Stream has no orientation" << endl;
	else if (retVal > 0)
		cout << "Stream is wide-oriented" << endl;
	else
		cout << "Stream is byte-oriented" << endl;

	return 0;
}

When you run the program, the output will be:

Stream has no orientation
Setting stream to wide-orientation
Stream is wide-oriented