std:: fwide
From cppreference.net
C++
Input/output library
| I/O manipulators | ||||
| Print functions (C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
|
(C++23)
|
||||
|
(
C++98/26*
)
|
||||
|
(C++20)
|
||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(
C++98/26*
)
|
||||
|
(
C++98/26*
)
|
||||
|
(
C++98/26*
)
|
||||
| Synchronized Output | ||||
|
(C++20)
|
||||
| Types | ||||
| Error category interface | ||||
|
(C++11)
|
||||
|
(C++11)
|
C-style I/O
| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<cwchar>
|
||
|
int
fwide
(
std::
FILE
*
stream,
int
mode
)
;
|
||
如果 mode > 0 ,则尝试使 stream 成为宽面向。如果 mode < 0 ,则尝试使 stream 成为字节面向。如果 mode == 0 ,则仅查询流的当前面向模式。
如果流的定向已经确定(通过执行输出操作或先前调用 fwide),此函数不执行任何操作。
目录 |
参数
| stream | - | 指向待修改或查询的C I/O流的指针 |
| mode | - | 整数值:大于零时设置流为宽模式,小于零时设置流为窄模式,等于零时仅进行查询 |
返回值
若调用后流为宽定向则返回大于零的整数,若调用后流为字节定向则返回小于零的整数,若流无定向则返回零。
示例
以下代码设置并重置流定向。
运行此代码
#include <cstdio> #include <cstdlib> #include <cwchar> #include <iostream> void show_orientation(int n) { n < 0 ? std::wcout << "\tnarrow orientation\n" : n > 0 ? std::wcout << "\twide orientation\n" : std::wcout << "\tno orientation\n"; } void try_read(FILE* fp) { if (const int c = std::fgetc(fp); c == EOF) std::wcout << "\tnarrow character read failed\n"; else std::wcout << "\tnarrow character read '" << static_cast<char>(c) << "'\n"; if (const wint_t wc = std::fgetwc(fp); wc == WEOF) std::wcout << "\twide character read failed\n"; else std::wcout << "\twide character read '" << static_cast<wchar_t>(wc) << "'\n"; } int main() { enum fwide_orientation : int { narrow = -1, query, wide }; FILE* fp = std::fopen("main.cpp", "r"); if (!fp) { std::wcerr << "fopen() failed\n"; return EXIT_FAILURE; } std::wcout << "1) A newly opened stream has no orientation.\n"; show_orientation(std::fwide(fp, fwide_orientation::query)); std::wcout << "2) Establish byte orientation.\n"; show_orientation(std::fwide(fp, fwide_orientation::narrow)); try_read(fp); std::wcout << "3) Only freopen() can reset stream orientation.\n"; if (std::freopen("main.cpp", "r", fp) == NULL) { std::wcerr << "freopen() failed\n"; return EXIT_FAILURE; } std::wcout << "4) A reopened stream has no orientation.\n"; show_orientation(std::fwide(fp, fwide_orientation::query)); std::wcout << "5) Establish wide orientation.\n"; show_orientation(std::fwide(fp, fwide_orientation::wide)); try_read(fp); std::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 '#'
参见
|
打开文件
(函数) |
|
|
C 文档
关于
fwide
|
|