fwide
来自 cppreference.cn
定义于头文件 <wchar.h> |
||
int fwide( FILE* stream, int mode ); |
(since 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: TBD)
- 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) |
打开文件 (函数) |
C++ 文档 for fwide
|