fgetc : fgetc « stdio.h « C Tutorial






ItemValue
Header#include
Declarationint fgetc(FILE *stream);
Functiongets the next character from the stream and increments the file position pointer.
ReturnEOF: if the end of the file is reached.


  1. When working with binary files
  2. use feof() to check for the end of the file.
  3. use ferror() to check for file errors.
#include <stdio.h>
  #include <stdlib.h>

  int main(int argc, char *argv[])
  {
    FILE *fp;
    char ch;

    if((fp=fopen("test","r"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
    }

    while((ch=fgetc(fp)) != EOF) {
      printf("%c", ch);
    }
    fclose(fp);

    return 0;
  }








22.6.fgetc
22.6.1.fgetc