Library Function clearerr in Stdio Class
Library Function clearerr of stdio.h Header File in C
On this page we will discuss about library function clearerr in stdio class which is used in C. The stdio.h is a header file in the C standard library that contains functions for reading and writing to files, as well as functions for performing input and output operations in C. The clearerr function in C is used to clear the error and end-of-file (EOF) indicators for a stream.
Library Function clearerr in Stdio Class
- In C programming language the clearerr function is included in stdio.h header file.
- The error indicator for a stream is set when a read or write operation on the stream fails. The EOF indicator is set when an end-of-file is encountered while reading from a stream. When either of these indicators is set, subsequent operations on the stream may fail or behave unexpectedly. The clearerr() function can be used to reset these indicators and allow the stream to be used again.
Declaration of clearerr function
void clearerr(FILE *stream);
Parameters of clearerr function
The clearerr() function in C takes a single parameter of type FILE*, which is a pointer to the stream for which the error and end-of-file (EOF) indicators should be cleared.
Parameter | Description |
---|---|
stream | It is a pointer to a FILE structure that represents a stream. This structure contains information about the stream, such as its current position, buffer, and state flags. |
Return value of clearerr function
Implementation of Library Function stdio.h clearerr
The following program shows the usage of clearerr() function.#include <stdio.h> int main() { // Open a file for reading FILE* fp = fopen("myfile.txt", "r"); if (fp == NULL) { // An error occurred while opening the file perror("Error opening file"); return 1; } // Read a string from the file char str[100]; while (fgets(str, 100, fp) != NULL) { // Print the string to the console printf("%s", str); } // Check if an error occurred or end-of-file was reached if (ferror(fp)) { // An error occurred while reading the file printf("An error occurred while reading the file.\n"); // Clear the error indicator for the stream clearerr(fp); } else if (feof(fp)) { // End-of-file was reached printf("End-of-file was reached.\n"); // Clear the EOF indicator for the stream clearerr(fp); } // Close the file fclose(fp); return 0; }
Output:
PrepInsta Prime End-of-file was reached.
In this example, an existing file myfile.txt is opened for reading and the fgets() function is used in a loop to read lines from the file myfile.txt until the end-of-file is reached.
If fgets() returns NULL, the ferror() and feof() functions are used to check whether an error occurred or the end-of-file was reached. If either of these conditions is true, the clearerr() function is called to clear the appropriate indicator for the stream. This allows the stream to be used again if desired.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment