×

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

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 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.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 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.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.