C programming stdio.h function - void clearerr(FILE *stream)
In C programming, the stdio.h header file provides a set of functions for performing input and output operations on streams of data. One of the functions in this header file is clearerr(), which clears the end-of-file and error indicators for a given file stream.
The clearerr() function takes one argument:
void clearerr(FILE *stream);ecruoS:www.theitroad.com
The argument, stream, is a pointer to a FILE object that represents the file stream to be cleared.
The clearerr() function clears the end-of-file and error indicators for the specified file stream, making it ready for further I/O operations. This is useful when you want to check for errors on a file stream that has already had an error or end-of-file condition.
Here's an example of how to use clearerr() to clear the error and end-of-file indicators on a file stream:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
/* Perform some I/O operations on the file stream */
if (feof(fp)) {
printf("End of file reached.\n");
clearerr(fp);
}
if (ferror(fp)) {
printf("Error occurred.\n");
clearerr(fp);
}
/* Perform more I/O operations on the file stream */
fclose(fp);
return 0;
}
In the above example, the fopen() function is used to open a file named "example.txt" in read mode. The feof() and ferror() functions are then used to check for end-of-file and error conditions on the file stream. If either condition is true, the clearerr() function is called to clear the indicators, and allow for further I/O operations on the file stream.
