文字
分享

在头文件<stdio.h>中定义



int fgetc(FILE * stream);



int getc(FILE * stream);



从给定的输入流中读取下一个字符。getc()可能被实现为一个宏。

参数

-

从中读取人物

返回值

获得成功或EOF失败的性格。

如果故障是由文件结束条件引起的,则另外设置eof指示器(参见feof()stream。如果故障是由其他错误引起的,请设置错误指示器(参见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标准(ISO / IEC 9899:2011):

    • 7.21.7.1 fgetc函数(p:330)

    • 7.21.7.5 getc函数(p:332)

  • C99标准(ISO / IEC 9899:1999):

    • 7.19.7.1 fgetc函数(p:296)

    • 7.19.7.5 getc函数(p:297-298)

  • C89 / C90标准(ISO / IEC 9899:1990):

    • 4.9.7.1 fgetc函数

    • 4.9.7.5 getc函数