fwide
来自 cppreference.cn
在头文件 <wchar.h> 中定义 |
||
int fwide( FILE* stream, int mode ); |
(自 C95 起) | |
如果 mode > 0,尝试使 stream 宽字符定向。如果 mode < 0,尝试使 stream 字节定向。如果 mode == 0,则只查询流的当前定向。
如果流的定向已经确定(通过执行输出或先前调用 fwide
),此函数不执行任何操作。
目录 |
[编辑] 参数
stream | - | 指向要修改或查询的 C I/O 流的指针 |
mode | - | 整数值,大于零表示将流设置为宽字符,小于零表示将流设置为字节,或零表示仅查询 |
[编辑] 返回值
如果在此调用后流是宽字符定向的,则返回一个大于零的整数;如果在此调用后流是字节定向的,则返回一个小于零的整数;如果流没有定向,则返回零。
[编辑] 示例
以下代码设置和重置流的定向。
运行此代码
#include <stdio.h> #include <stdlib.h> #include <wchar.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); c == EOF ? printf("\tnarrow character read failed\n") : printf("\tnarrow character read '%c'\n", c); wint_t wc = fgetwc(fp); wc == WEOF ? printf("\twide character read failed\n") : 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 '#'
[编辑] 参考
- C23 标准 (ISO/IEC 9899:2024)
- 7.29.3.5 fwide 函数(p:待定)
- C17 标准 (ISO/IEC 9899:2018)
- 7.29.3.5 fwide 函数(p:309)
- C11 标准 (ISO/IEC 9899:2011)
- 7.29.3.5 fwide 函数(p:423)
- C99 标准 (ISO/IEC 9899:1999)
- 7.24.3.5 fwide 函数(p:369)
[编辑] 另请参阅
(C11) |
打开文件 (function) |
C++ documentation for fwide
|