×

Ferror() in c

Ferror():

 In C, the ferror() function checks assuming there is a blunder in the given stream.

The ferror() function is utilized to check for the document blunder on given stream. A return worth of zero shows no mistake has happened, while a non-zero worth demonstrates a blunder happened.

Syntax for ferror() function:

Int ferror(FILE *filename);

The parameters which we use in ferror() function:

FILE *Filename;

The Return type:

Int(0 or 1)

The ferror() function returns a nonzero worth to show a mistake on the given stream . A return worth of 0 implies that no mistake has happened. Model that utilizes ferror() This model puts information out to a stream, and afterward checks that a compose blunder has not happened.

Let’s see the uses of the ferror function:

Assuming in the last document activity there is some blunder presented in that record, by the ferror() capability we distinguish there is some mistake in that document during the last activity. The model of ferror() capability is declared as:

Int ferror(FILE *filename);

In any case, just identifies the last functional blunder of the record on the grounds that each time the mistake banner of the document is set.

Lets see an example program for the ferror () function:

Example1:

#include <stdio.h>
#include <stdlib.h>
 
Int main()
{
    FILE* x;
    Char str[100];
    If ((f = fopen(“sample.txt”, “r”)) == NULL) {
        Printf(“the file cant be open”);
        Exit(1);
    }
    If (ferror(f))
        Printf(“Having Error to read file\n”);
    Else
        Printf(“Not having any error to read file\n”);
 
    Printf(“The result is:\n”);
    While (!feof(x)) {
        Fgets(str, 100, x):;
        Printf(“%s”, str);


    Fclose(f);
    Return 0;
}

Lets see the output for this program.

Javatpoint
Tutorials and examples
Thank you for using javatpoint

The following output which we given from the command prompt.

Lets see another example

Example 2

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


int main () {
   FILE *fp;
   char ch;


   fp = fopen("sample.txt", "w");


   ch = fgetc(fp);
   if( ferror(fp) ) {
      printf(" The Error is in reading from the file : sample.txt\n");
   }
   clearerr(fp);
   
   if( ferror(fp) ) {
      printf(" The Error is in reading from the file : sample.txt\n");
   }
   fclose(fp);


   return(0);
}

Expecting we have a text record sample.txt, which is an unfilled document. Allow us to gather and run the above program that will create the accompanying outcome since we attempt to peruse a document which we opened in compose just mode.

The purpose of Ferror()

The ferror() function tests for a blunder in perusing from or keeping in touch with the given stream . On the off chance that a blunder happens, the mistake pointer for the stream stays set until you close stream , call the rewind() function, or call the clearerr() function.

Example 3:

#include <stdio.h>
int main()
{
  FILE * fp;
  fp = fopen("sample.txt","r");  
  char buff[]; 50


  fgets(buff, 100, fp);




  if(ferror(fp)) {
    printf(" The Error gets from reading the file!\n");
  }
  else {
    printf(" The sample file content: %s \n", buff);
  }
  fclose(fPointer);




  fPointer = fopen("ferrorExample.txt","w");




  fgets(buff, 100, fPointer);




  if(ferror(fp)) {
    printf("The Error gets from reading the file!\n");
  }
  else {
    printf("The sample file content: %s \n", buff);
  }




  fclose(fp);




  return 0;
}

Output:

The sample file content: ferror program

The Error is gets fromreading from file

  • Lines 6-19: The fopen() function is utilized in line 6 to open a document in read mode. The substance of the record is perused in buff utilizing fgets(). The substance of the document is perused effectively. ferror() then returns 0 and the in the event that condition in line 12 isn't fulfilled. At last, the record content is imprinted on the screen.
  • lines 21-32: The fopen() function is utilized in line 21 to open a document in compose mode. fgets() endeavors to peruse the substance of the document.
  •  This is ineffective. The mistake is given utilizing ferror() as perused activity is applied to the document opened in compose mode. ferror() then, at that point, returns non-zero worth, the assuming condition in line 25 is fulfilled, and a mistake message is imprinted on the screen.

Lets see the overview of ferror() function in c:

  • The ferror() function is utilized to check for the document blunder on given stream. A return worth of zero shows no mistake has happened, while a non-zero worth demonstrates a blunder happened.
  • The ferror() function returns a nonzero worth to show a mistake on the given stream . A return worth of 0 implies that no mistake has happened. Model that utilizes ferror() This model puts information out to a stream, and afterward checks that a compose blunder has not happened.
    Int ferror(FILE *filename);

Related Topics

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Dangling pointers in C

Dangling pointers in C: A pointer is a variable that stores the memory address of other variables. The pointers may also store the address of other’s memory items. They are...

4 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.