Showing posts with label ferror function. Show all posts
Showing posts with label ferror function. Show all posts

Friday, October 16, 2020

, ,

C program for ferror() function

 ferror() function in C Programming language is used to identify the error in given c file. 

C program for ferror() function
C program for ferror()

 

In this tutorial you will learn about how to use the ferror() function and programing syntax.

 Prototype :-  

 int ferror(FILE *filename);

Parameters :-

FILE *filename

C program for ferror() function

#include <stdio.h>
#include <stdlib.h>

int main()
{
	FILE *fr;
	char str[100];

	if((f=fopen("help.txt","r"))==NULL){
		printf("Cannot open the file...");
		//file is not exist program is terminated
		exit(1);
	}

	// Check if here is some error in the file
	if(ferror(f))
		printf("print the Error to read the file\n");
	else	
		printf("No error\n");

	printf("File content is--\n");
	//print the strings until EOF is encountered
	while(!feof(fr)){
		fgets(str,100,fr);
		//print the string
		printf("%s",str);
	}

	//remember to close the opened file
	fclose(f);

	return 0;
}