std::fgetc, std::getc
来自 cppreference.cn
定义于头文件 <cstdio> |
||
从给定的输入流中读取下一个字符。
目录 |
[编辑] 参数
stream | - | 要读取字符的流 |
[编辑] 返回值
成功时返回获取的字符,失败时返回 EOF。
如果失败是由于文件结束条件造成的,则额外设置 stream 上的 *eof* 指示器(参阅 std::feof())。如果失败是由于其他错误造成的,则设置 stream 上的 *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; // Note: int, not char, required to handle EOF while ((c = std::fgetc(fp)) != EOF) // Standard C I/O file reading loop 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
|