Question: What are Stream Error Functions in C Language?
Answer: We have functions in ANSI C specification which are used to determine the state of a stream. These are prototyped below,
int feof(FILE *stream); int ferror(FILE *stream); void clearerr(FILE *stream);
‘feof()’ returns TRUE if stream is currently at end of file. Well! We have already seen that we can change current position in the file using fseek(), rewind() or fsetpos() functions. For ex.
To set to the beginning in a given stream,
stream = fopen("file", "r"); rewind(stream);
or we can use
advertisement
advertisement
fseek(stream, 0, SEEK_SET);
‘ferror()’ reports on error state of stream and returns TRUE if any read/write errors have occurred.
Take C Programming Tests Now!
/* ferror.c */ #include <stdio.h> #include <stdlib.h> #define SIZE 10 int main(void) { FILE *stream; char buffer[SIZE]; stream = fopen("hello.txt", "r"); if (stream == NULL) { perror("File Open"); exit(EXIT_FAILURE); } /* read from the stream into buffer */ fgets(buffer, SIZE, stream); /* check if there were any read/erite errors */ if (ferror(stream)) printf("Read/ Write Error.\n"); return 0; }
Finally, ‘clearerr()’ resets error indication on a given stream.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
advertisement
If you wish to look at all C Tutorials, go to C Tutorials.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for C Internship
- Practice BCA MCQs
- Buy Computer Science Books
- Apply for Computer Science Internship
- Buy C Books