🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
clearerr() function in C is a standard library function that clears the error and end-of-file indicators for the specified stream. It is part of the C standard library (stdio.h) and is used to reset the error and EOF states of a stream.clearerr() Function Syntax
The syntax for the clearerr() function is as follows:
void clearerr(FILE *stream);
Parameters:
stream: A pointer to aFILEobject that specifies the stream for which the error and end-of-file indicators should be cleared.
Returns:
- The
clearerr()function does not return a value.
Examples
Clearing Error Indicators for a File
To demonstrate how to use clearerr() to clear the error indicators for a file, we will write a simple program.
Example
#include <stdio.h>
int main() {
FILE *file;
int ch;
// Open the file for reading
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error: Could not open file for reading.\n");
return 1;
}
// Attempt to read past the end of the file to set the EOF indicator
while ((ch = fgetc(file)) != EOF) {
// Do nothing, just read until EOF
}
// Check if EOF indicator is set
if (feof(file)) {
printf("End of file reached.\n");
}
// Clear the EOF indicator
clearerr(file);
// Check if EOF indicator is cleared
if (!feof(file)) {
printf("EOF indicator cleared.\n");
}
// Close the file
fclose(file);
return 0;
}
Output:
End of file reached.
EOF indicator cleared.
Clearing Error Indicators for Standard Input
This example shows how to use clearerr() to clear the error indicators for the standard input stream.
Example
#include <stdio.h>
int main() {
int ch;
// Simulate an input error by forcing an EOF
printf("Enter some text (press Ctrl+D to simulate EOF): ");
while ((ch = getchar()) != EOF) {
putchar(ch);
}
// Check if EOF indicator is set
if (feof(stdin)) {
printf("\nEnd of input reached.\n");
}
// Clear the EOF indicator
clearerr(stdin);
// Check if EOF indicator is cleared
if (!feof(stdin)) {
printf("EOF indicator cleared. You can enter more text now.\n");
}
// Read and print more input to demonstrate that the indicator is cleared
while ((ch = getchar()) != '\n' && ch != EOF) {
putchar(ch);
}
return 0;
}
Output (after pressing Ctrl+D and then entering more text):
Enter some text (press Ctrl+D to simulate EOF): Hello
Hello
End of input reached.
EOF indicator cleared. You can enter more text now.
More text
Real-World Use Case
Robust File Handling
In real-world applications, the clearerr() function can be used to implement robust file handling routines where the program needs to recover from errors or handle end-of-file conditions gracefully.
Example
#include <stdio.h>
void process_file(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Error: Could not open file %s for reading.\n", filename);
return;
}
int ch;
while ((ch = fgetc(file)) != EOF) {
// Simulate processing each character
if (ch == 'x') { // Simulate an error condition
printf("Encountered 'x', resetting stream...\n");
clearerr(file);
// Normally you might handle the error or retry reading
break;
}
putchar(ch);
}
if (feof(file)) {
printf("\nEnd of file reached.\n");
}
if (ferror(file)) {
printf("\nAn error occurred while reading the file.\n");
}
fclose(file);
}
int main() {
process_file("example.txt");
return 0;
}
Output (assuming example.txt contains the text "Hello, World!"):
Hello, World!
Conclusion
The clearerr() function is used to clear the error and end-of-file indicators for a specified stream. This can be useful in situations where you want to reset the state of a stream after handling an error or reaching the end of the file.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment