
- The C Standard Library
- C Library - Home
- C Library - <assert.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <float.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdarg.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <time.h>
- C Standard Library Resources
- C Library - Quick Guide
- C Library - Useful Resources
- C Library - Discussion
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C library function - ferror()
Description
The C library function int ferror(FILE *stream) tests the error indicator for the given stream.
Declaration
Following is the declaration for ferror() function.
int ferror(FILE *stream)
Parameters
stream − This is the 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 else, it returns a zero value.
Example
The following example shows the usage of ferror() function.
#include <stdio.h> int main () { FILE *fp; char c; fp = fopen("file.txt", "w"); c = fgetc(fp); if( ferror(fp) ) { printf("Error in reading from file : file.txt\n"); } clearerr(fp); if( ferror(fp) ) { printf("Error in reading from file : file.txt\n"); } fclose(fp); return(0); }
Assuming we have a text file file.txt, which is an empty file. Let us compile and run the above program that will produce the following result because we try to read a file which we opened in write only mode.
Error reading from file "file.txt"
stdio_h.htm
Advertisements