当前位置: C语言 -- 标准库 -- <wchar.h> -- fwide

fwide函数


概要:
#include <stdio.h>
#include <wchar.h>
int fwide(FILE *stream, int mode);

描述:

该函数确定参数stream指向流的导向(the orientation of the stream)。

当流和外部文件关联后,未进行任何操作前,流是没有导向的。当第一个输入/输出函数作用于流时就会自动设置流的导向;如果是字节输入/输出函数,流将设置成字节导向流;如果是宽字符输入/输出函数,流将设置成宽字符导向流。

如果流的导向已经确定,fwide函数不会改变流的导向(只有调用freopen函数后,才能改变流的导向。)。如果流的导向没有确定,fwide函数将根据参数mode的值改变流的导向。

调用fwide函数可以在输入/输出操作之前显式地改变流的导向。


参数:
FILE *stream

FILE类型指针,指向一个打开的流。

int mode

指定流导向的整数。如果mode值为0,不改变流导向;如果mode值大于0,尝试将流设置成宽字符导向流;如果mode值小于0,尝试将流设置成字节导向流。


返回值:

调用函数后,如果流为宽字符导向流,将返回一个大于0的值;如果流为字节导向流,将返回一个小于0的值;如果流没有导向,将返回0


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
/*函数fwide范例*/

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>

void func(FILE *pFile)
{
    int orientation;

    orientation = fwide(pFile, 0);

    /*判断流导向。*/
    if(orientation > 0)
        puts("The stream is a wide-oriented stream.");
    else if(orientation == 0)
        puts("The stream is without orientation.");
    else
        puts("The stream is a byte-oriented stream.");
}

int main(void)
{
    setlocale(LC_CTYPE, "");

    FILE *pFile;

    /*创建文件。*/
    pFile = fopen("gch.txt", "w");
    if(!pFile)
    {
        perror("Error");
        exit(EXIT_FAILURE);
    }

    /*向文件中写入内容。*/
    fputws(L"A good medicine tasks bitter.", pFile);

    /*判断流导向。*/
    func(pFile);

    /*关闭文件。*/
    fclose(pFile);
    
    return 0;
}


输出:

The stream is a wide-oriented stream.

:使用Pelles C编译。


相关内容:
fopen 打开文件的函数。
freopen 重新打开文件的函数。