Home » C programs » stdio.h header file functions

ferror() function in C language with Example

Here, we are going to learn about the ferror() function of library header stdio.h in C language with its syntax, example.
Submitted by Souvik Saha, on January 06, 2019

ferror() function in C


    int ferror(FILE *filename);


    FILE *filename

Return type: int(0 or 1)

Use of function:

If in the last file operation there is some error introduced in that file, by the ferror() function we identify there is some error in that file during the last operation. The prototype of ferror() function is int ferror(FILE* filename);

But only detects the last operational error of the file because every time the error flag of the file is set.

ferror() example in C

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

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

	//Check the existence of that file
		printf("Cannot open the file...");
		//if not exist program is terminated

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

	printf("File content is--\n");
	//print the strings until EOF is encountered
		//print the string

	//close the opened file

	return 0;


ferror example in c

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.