std:: fgetc, std:: getc
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<cstdio>
|
||
|
int
fgetc
(
std::
FILE
*
stream
)
;
int getc ( std:: FILE * stream ) ; |
||
从给定输入流中读取下一个字符。
目录 |
参数
| stream | - | 读取字符的来源 |
返回值
成功时获取的字符或失败时返回 EOF 。
若失败由文件结束条件引起,则额外设置流上的 eof 指示符(参见 std::feof() )。若失败由其他错误引起,则设置流上的 error 指示符(参见 std::ferror() )。
示例
运行此代码
#include <cstdio> #include <cstdlib> int main() { int is_ok = EXIT_FAILURE; FILE* fp = std::fopen("/tmp/test.txt", "w+"); if (!fp) { std::perror("File opening failed"); return is_ok; } int c; // 注意:必须使用 int 而非 char 来处理 EOF while ((c = std::fgetc(fp)) != EOF) // 标准 C I/O 文件读取循环 std::putchar(c); if (std::ferror(fp)) std::puts("I/O error when reading"); else if (std::feof(fp)) { std::puts("End of file reached successfully"); is_ok = EXIT_SUCCESS; } std::fclose(fp); return is_ok; }
输出:
End of file reached successfully
参见
|
(C++11 中弃用)
(C++14 中移除)
|
从
stdin
读取字符串
(函数) |
|
向文件流写入一个字符
(函数) |
|
|
将一个字符退回文件流
(函数) |
|
|
C 文档
关于
fgetc
,
getc
|
|