fwide

From cppreference.com
< c‎ | io
 
 
File input/output


Functions
File access
fwide
(C95)
Direct input/output
Unformatted input/output
(C95)(C95)
(C95)
(C95)(C95)
(C95)
(C95)
Formatted input
Formatted output
File positioning
Error handling
Operations on files
 
Defined in header <wchar.h>
int fwide( FILE *stream, int mode );
(since C95)

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

Parameters

stream - pointer to the C I/O stream to modify or query
mode - 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

An integer greater than zero if the stream is wide-oriented after this call, less than zero if the stream is byte-oriented after this call, and zero if the stream has no orientation.

Example

The following code sets and resets the stream orientation.

#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
void try_read(FILE* fp)
{
    int c = fgetc(fp);
    if(c == EOF) puts("narrow character read failed");
    else printf("narrow character read '%c'\n", c);
 
    wint_t wc = fgetwc(fp);
    if(wc == WEOF) puts("wide character read failed");
    else printf("wide character read '%lc'\n", wc);
}
void show(int n)
{
    if(n == 0) puts("no orientation");
    else if (n < 0) puts("narrow orientation");
    else puts("wide orientation");
}
int main(void)
{
    FILE* fp = fopen("main.cpp","r");
    if (!fp) {
        perror("fopen() failed");
        return EXIT_FAILURE;
    }
 
    // A newly opened stream has no orientation.
    show(fwide(fp, 0));
 
    // Establish byte orientation.
    show(fwide(fp, -1));
    try_read(fp);
 
    // Only freopen() can reset stream orientation.
    if (freopen("main.cpp","r",fp) == NULL)
    {
       perror("freopen() failed");
       return EXIT_FAILURE;
    }
 
    // A reopened stream has no orientation.
    show(fwide(fp, 0));
 
    // Establish wide orientation.
    show(fwide(fp, 1));
    try_read(fp);
 
    fclose(fp);
}

Possible output:

no orientation
narrow orientation
narrow character read '#'
wide character read failed
no orientation
wide orientation
narrow character read failed
wide character read '#'

References

  • C11 standard (ISO/IEC 9899:2011):
  • 7.29.3.5 The fwide function (p: 423)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.24.3.5 The fwide function (p: 369)

See also

opens a file
(function)