YouTip LogoYouTip

C Function Clearerr

# C Library Function - clearerr() The `clearerr()` function is a built-in function in the C standard library (``) used to reset the error and End-of-File (EOF) indicators for a given input/output stream. When performing file operations in C, internal flags are set when an error occurs or when the end of a file is reached. These flags remain set until they are explicitly cleared. The `clearerr()` function allows developers to reset these flags so that subsequent I/O operations can proceed or be tested accurately. --- ## Syntax ```c void clearerr(FILE *stream); ``` ### Parameters * **`stream`**: This is a pointer to a `FILE` object that identifies the input/output stream. ### Return Value The `clearerr()` function does not return any value (it returns `void`). It cannot fail under normal usage and does not modify the global `errno` variable. *(Note: In some non-standard or platform-specific implementations, passing an invalid file pointer may result in undefined behavior or set `errno` to `EBADF`, but according to the ANSI C standard, the function has no return value).* --- ## Why Use `clearerr()`? When reading or writing to a file, two internal status flags are associated with the stream: 1. **EOF Flag**: Set when an operation reaches the end of the file. Checked using `feof()`. 2. **Error Flag**: Set when an I/O error occurs (e.g., trying to read from a write-only stream). Checked using `ferror()`. Once either of these flags is set, subsequent read/write operations on that stream may fail or behave unexpectedly until the flags are cleared. Calling `clearerr()` resets both flags back to zero (false). --- ## Code Example The following example demonstrates how `clearerr()` works. In this program, we open a file in write-only mode (`"w"`) and then attempt to read from it using `fgetc()`. This illegal operation triggers the stream's error flag. We then use `clearerr()` to reset the error state. ```c #include int main() { FILE *fp; char c; // Open the file in write-only mode fp = fopen("file.txt", "w"); if (fp == NULL) { perror("Error opening file"); return -1; } // Attempt to read from a write-only stream (this will trigger an error) c = fgetc(fp); // Check if an error occurred if (ferror(fp)) { printf("Error occurred while reading 'file.txt'.\n"); } // Clear the error and EOF indicators clearerr(fp); // Check the error status again if (ferror(fp)) { printf("Error flag is still set.\n"); } else { printf("Error flag successfully cleared.\n"); } fclose(fp); return 0; } ``` ### Output When you compile and run the program above, it will produce the following output: ```text Error occurred while reading 'file.txt'. Error flag successfully cleared. ``` --- ## Important Considerations * **No Automatic Recovery**: Clearing the error flag with `clearerr()` does not fix the underlying physical or system error (e.g., a disconnected drive or permission issue). It only resets the software flag in the `FILE` structure. * **Alternative Functions**: Functions like `rewind(stream)` also implicitly clear the EOF and error indicators for the specified stream. * **Use Cases**: `clearerr()` is highly useful in interactive console applications or network streams where temporary errors (like timeouts or interrupts) occur, allowing you to retry the operation after clearing the error state.
← C Function FgetposC Function Fclose β†’