非常教程

C参考手册

文件输入/输出 | File input/output

fwide

在头文件<wchar.h>中定义

int fwide(FILE * stream,int mode);

(自C95以来)

如果mode > 0,试图使stream广泛导向。如果mode < 0,试图使stream字节为导向。如果mode==0仅查询流的当前方向。

如果流的方向已经确定(通过执行输出或通过先前调用fwide),则此函数不执行任何操作。

参数

-

指向要修改或查询的CI / O流的指针

模式

-

大于零的整数值将流设置为宽度,小于零以将流设置为窄,或者仅将零设置为仅查询

返回值

如果流在此调用后面向全局,则为大于零的整数,如果流在此调用后面向字节,则小于零;如果流没有定向,则为零。

以下代码设置并重置流的方向。

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

可能的输出:

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

参考

  • C11标准(ISO / IEC 9899:2011):
    • 7.29.3.5 fwide函数(p:423)
  • C99标准(ISO / IEC 9899:1999):
    • 7.24.3.5 fwide函数(p:369)
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。