C Function Ferror
# C Library Function - ferror()
[ C Standard Library - ](#)
## Description
The C library function **int ferror(FILE *stream)** tests the error indicator for the given stream.
## Declaration
Below is the declaration for the ferror() function.
int ferror(FILE *stream)
## Parameters
* **stream** -- This is a pointer to a FILE object that identifies the stream.
## Return Value
If the error indicator associated with the stream was set, the function returns a non-zero value, otherwise, it returns a zero value.
## Example
The following example demonstrates the usage of the ferror() function.
#include int main(){ FILE *fp; char c; fp = fopen("file.txt", "w"); c = fgetc(fp); if( ferror(fp) ) { printf("Error reading from file: file.txtn"); } clearerr(fp); if( ferror(fp) ) { printf("Error reading from file: file.txtn"); } fclose(fp); return(0);}
Let us assume we have a text file **file.txt**, which is an empty file. Let us compile and run the above program, as we are trying to read from a file opened in write-only mode, this will produce the following result.
Error reading from file: file.txt
[ C Standard Library - ](#)
YouTip