C – Reading File Character by Character

Reading File character by character:

  • fgetc() is a  pre-defined function belongs to stdio.h
  • fgetc() function read the input file character by character.
  • On success, it reads the character and returns ASCII value of that character
  • On Failure, it returns -1(EOF)

int  fgetc(FILE* stream);

#include<stdio.h>
int main(){
            FILE* p;
            int ch;  
            p = fopen(“code.c”, “r”);
            if(p==NULL)
            {
                        printf(“No such file to open \n”);
            }
            else
            {
                        printf(“File contents : \n”);
                        while((ch=fgetc(p)) != -1)
                        {
                                    printf(“%c”, ch);           
                        }
            }
            return 0;
}
Scroll to Top