Write a program to read the data character by character from a file

Previous
Next

Reading Data from file:

  • 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)
  • Prototype is :
    • 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;
}
Previous
Next

Courses Enquiry Form