std::fgetc, std::getc
來自 cppreference.com
定義於標頭檔案 <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
|