?
This document uses PHP Chinese website manual Release
在頭文件<stdio.h>中定義 | ||
---|---|---|
int fgetc(FILE * stream); | ||
int getc(FILE * stream); |
從給定的輸入流中讀取下一個(gè)字符。getc()
可能被實(shí)現(xiàn)為一個(gè)宏。
流 | - | 從中讀取人物 |
---|
獲得成功或EOF
失敗的性格。
如果故障是由文件結(jié)束條件引起的,則另外設(shè)置eof指示器(參見(jiàn)feof()
)stream
。如果故障是由其他錯(cuò)誤引起的,請(qǐng)?jiān)O(shè)置錯(cuò)誤指示器(參見(jiàn)ferror()
)stream
。
#include <stdio.h>#include <stdlib.h> int main(void){ FILE* fp = fopen("test.txt", "r"); if(!fp) { perror("File opening failed"); return EXIT_FAILURE; } int c; // note: int, not char, required to handle EOF while ((c = fgetc(fp)) != EOF) { // standard C I/O file reading loop putchar(c); } if (ferror(fp)) puts("I/O error when reading"); else if (feof(fp)) puts("End of file reached successfully"); fclose(fp);}
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.21.7.1 fgetc函數(shù)(p:330)
7.21.7.5 getc函數(shù)(p:332)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.19.7.1 fgetc函數(shù)(p:296)
7.19.7.5 getc函數(shù)(p:297-298)
C89 / C90標(biāo)準(zhǔn)(ISO / IEC 9899:1990):
4.9.7.1 fgetc函數(shù)
4.9.7.5 getc函數(shù)