fwide

From cppreference.com
< c‎ | io
 
 
File input/output
Types and objects
Functions
File access
(哋它亢11)
(哋它亢11)
fwide
(哋它亢95)
Direct input/output
Unformatted input/output
(until 哋它亢11)(哋它亢11)
(哋它亢95)(哋它亢95)
(哋它亢95)
(哋它亢95)(哋它亢95)
(哋它亢95)
(哋它亢95)
(哋它亢95)
(哋它亢95)
Formatted input
(哋它亢11)(哋它亢11)(哋它亢11)
(哋它亢95)(哋它亢95)(哋它亢95)(哋它亢11)(哋它亢11)(哋它亢11)    
(哋它亢99)(哋它亢99)(哋它亢99)(哋它亢11)(哋它亢11)(哋它亢11)
(哋它亢99)(哋它亢99)(哋它亢99)(哋它亢11)(哋它亢11)(哋它亢11)     
Formatted output
(哋它亢99)(哋它亢11)(哋它亢11)(哋它亢11)(哋它亢11)
(哋它亢95)(哋它亢95)(哋它亢95)(哋它亢11)(哋它亢11)(哋它亢11)(哋它亢11)    
(哋它亢99)(哋它亢11)(哋它亢11)(哋它亢11)(哋它亢11)
(哋它亢95)(哋它亢95)(哋它亢95)(哋它亢11)(哋它亢11)(哋它亢11)(哋它亢11)
File positioning
Error handling
Operations on files
(哋它亢11)
(哋它亢11)
 
Defined in header <wchar.h>
int fwide( FILE *stream, int mode );
(since 哋它亢95)

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 show_orientation(int n)
{
    n < 0 ? puts("\tnarrow orientation"):
    n > 0 ? puts("\twide orientation"):
            puts("\tno orientation");
}
 
void try_read(FILE* fp)
{
    int c = fgetc(fp);
    if(c == EOF)
        puts("\tnarrow character read failed");
    else
        printf("\tnarrow character read '%c'\n", c);
 
    wint_t wc = fgetwc(fp);
    if(wc == WEOF)
        puts("\twide character read failed");
    else
        printf("\twide character read '%lc'\n", wc);
}
 
int main(void)
{
    enum fwide_orientation { narrow = -1, query, wide };
 
    FILE* fp = fopen("main.cpp", "r");
    if (!fp)
    {
        perror("fopen() failed");
        return EXIT_FAILURE;
    }
 
    puts("1) A newly opened stream has no orientation.");
    show_orientation(fwide(fp, query));
 
    puts("2) Establish byte orientation.");
    show_orientation(fwide(fp, narrow));
    try_read(fp);
 
    puts("3) Only freopen() can reset stream orientation.");
    if (freopen("main.cpp", "r", fp) == NULL)
    {
       perror("freopen() failed");
       return EXIT_FAILURE;
    }
 
    puts("4) A reopened stream has no orientation.");
    show_orientation(fwide(fp, query));
 
    puts("5) Establish wide orientation.");
    show_orientation(fwide(fp, wide));
    try_read(fp);
 
    fclose(fp);
}

Possible output:

1) A newly opened stream has no orientation.
	no orientation
2) Establish byte orientation.
	narrow orientation
	narrow character read '#'
	wide character read failed
3) Only freopen() can reset stream orientation.
4) A reopened stream has no orientation.
	no orientation
5) Establish wide orientation.
	wide orientation
	narrow character read failed
	wide character read '#'

References

  • 哋它亢17 standard (ISO/IEC 9899:2018):
  • 7.29.3.5 The fwide function (p: 309)
  • 哋它亢11 standard (ISO/IEC 9899:2011):
  • 7.29.3.5 The fwide function (p: 423)
  • 哋它亢99 standard (ISO/IEC 9899:1999):
  • 7.24.3.5 The fwide function (p: 369)

See also

(哋它亢11)
opens a file
(function)