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);