clearerr() Function
The clearerr()
function in C resets the error and end-of-file indicators for a stream. This is particularly useful after an I/O function has failed or reached the end-of-file, allowing you to continue further operations on the stream.
Syntax of clearerr()
void clearerr(FILE *stream);
Parameters
Parameter | Description |
---|---|
stream | A pointer to a FILE object that identifies the stream whose error and end-of-file indicators are to be reset. |
Calling clearerr()
resets the stream’s internal indicators, making it possible to perform additional I/O operations as if no error or end-of-file condition had previously occurred. Note that functions such as rewind
, fseek
, fsetpos
, and freopen
also clear these indicators.
Return Value
The clearerr()
function does not return a value.
Examples for clearerr()
Example 1: Basic Usage of clearerr() to Reset Stream Indicators
This example demonstrates how to use clearerr()
to reset the error and end-of-file indicators after reaching the end of a file.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
int ch;
// Read the file until EOF is reached
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
// Check if EOF indicator is set
if (feof(fp)) {
printf("\nEnd-of-File reached.\n");
}
// Reset the error and EOF indicators
clearerr(fp);
// Rewind the file and read the first character again
rewind(fp);
ch = fgetc(fp);
if (ch != EOF) {
printf("After clearerr and rewind, first character: %c\n", ch);
}
fclose(fp);
return 0;
}
Explanation:
- The file
example.txt
is opened for reading. - The program reads characters until the end-of-file is reached, setting the EOF indicator.
clearerr()
is then called to reset these indicators.- The file is rewound and the first character is read to demonstrate that the stream is now ready for further operations.
Program Output:
[Contents of example.txt]
End-of-File reached.
After clearerr and rewind, first character: [First character of example.txt]
Example 2: Handling an Error Condition with clearerr()
This example shows how to handle an error condition when attempting to open a file and how clearerr()
is used even though the error condition persists from the failed operation.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("nonexistent.txt", "r");
if (fp == NULL) {
perror("File open error");
} else {
// Simulate a read error
int result = fgetc(fp);
if (result == EOF && ferror(fp)) {
printf("An error occurred during reading.\n");
}
clearerr(fp);
fclose(fp);
}
return 0;
}
Explanation:
- The program attempts to open a non-existent file
nonexistent.txt
, triggering an error. perror()
outputs an appropriate error message.- Even though
clearerr()
is called, the file pointer remains invalid as the file was never opened.
Program Output:
File open error: [Error details corresponding to the failed attempt]
Example 3: Using clearerr() After an fseek Operation
This example demonstrates using clearerr()
after an fseek()
operation that attempts to move the file pointer beyond the file’s content, which may set error indicators.
Program
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
// Attempt to move the file pointer beyond the end of the file
if (fseek(fp, 10000, SEEK_SET) != 0) {
perror("fseek error");
}
// Clear the error state caused by fseek
clearerr(fp);
// Rewind the file and read the first character
rewind(fp);
int ch = fgetc(fp);
if (ch != EOF) {
printf("After clearerr and rewind, first character: %c\n", ch);
}
fclose(fp);
return 0;
}
Explanation:
- The file
example.txt
is opened for reading. - An
fseek()
operation attempts to move the pointer beyond the file’s end, causing an error indicator to be set. clearerr()
is called to reset the error indicator.- The file is rewound and the first character is read, demonstrating that the stream is ready for further operations.
Program Output:
[Contents of example.txt]
After clearerr and rewind, first character: [First character of example.txt]