fwide

来自cppreference.com
< c‎ | io
 
 
文件输入/输出
类型与对象
函数
文件访问
fwide
(C95)
直接输入/输出
无格式输入/输出
(C95)(C95)
(C95)
(C95)(C95)
(C95)
(C95)
有格式输入
 
在标头 <wchar.h> 定义
int fwide( FILE *stream, int mode );
(C95 起)

mode > 0 ,则试图令 stream 为宽取向。若 mode < 0 ,则试图令 stream 为字节取向。若 mode==0 ,则只查询流的当前取向。

若已经决定流的取向(通过执行输出或通过之前调用 fwide ),则此函数不做任何事。

参数

stream - 指向修改或查询的 C I/O 流的指针
mode - 大于零的整数值设置流为宽,小于零的整数值设置流为窄,零为仅查询

返回值

若流在此调用后为宽取向,则为大于零的整数,若流在此调用后为字节取向,则为小于零的整数,若流无取向,则为零。

示例

下列代码设置并重置流的取向。

#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);
}

可能的输出:

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 '#'

引用

  • C17 标准(ISO/IEC 9899:2018):
  • 7.29.3.5 The fwide function (第 309 页)
  • C11 标准(ISO/IEC 9899:2011):
  • 7.29.3.5 The fwide function (第 423 页)
  • C99 标准(ISO/IEC 9899:1999):
  • 7.24.3.5 The fwide function (第 369 页)

参阅

打开文件
(函数)