🎓 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
feof() function in C is a standard library function that checks the end-of-file indicator for the given stream. It is part of the C standard library (stdio.h) and is commonly used to detect the end of a file during input operations.feof() Function Syntax
The syntax for the feof() function is as follows:
int feof(FILE *stream);
Parameters:
stream: A pointer to aFILEobject that specifies the stream to be checked.
Returns:
- The function returns a non-zero value if the end-of-file indicator is set for the specified stream. Otherwise, it returns
0.
Examples
Checking End-of-File for a File
To demonstrate how to use feof() to check the end-of-file indicator 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;
}
// Read characters until the end of the file
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
// Check if EOF indicator is set
if (feof(file)) {
printf("\nEnd of file reached.\n");
}
// Close the file
fclose(file);
return 0;
}
Output (assuming example.txt contains the text "Hello, World!"):
Hello, World!
End of file reached.
Handling End-of-File in a Loop
This example shows how to use feof() in a loop to handle the end-of-file condition explicitly.
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;
}
// Read and print characters until EOF is reached
while (1) {
ch = fgetc(file);
if (feof(file)) {
break;
}
putchar(ch);
}
printf("\nEnd of file reached.\n");
// Close the file
fclose(file);
return 0;
}
Output (assuming example.txt contains the text "Hello, World!"):
Hello, World!
End of file reached.
Real-World Use Case
Reading Lines from a File Until EOF
In real-world applications, the feof() function can be used to read lines from a file until the end of the file is reached.
Example
#include <stdio.h>
int main() {
FILE *file;
char line[256];
// Open the file for reading
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error: Could not open file for reading.\n");
return 1;
}
// Read and print lines until EOF is reached
while (fgets(line, sizeof(line), file) != NULL) {
printf("%s", line);
}
// Check if EOF indicator is set
if (feof(file)) {
printf("\nEnd of file reached.\n");
}
// Close the file
fclose(file);
return 0;
}
Output (assuming example.txt contains multiple lines of text):
First line
Second line
Third line
End of file reached.
Conclusion
The feof() function is useful for determining whether the end of a file has been reached during reading operations. It helps in managing input operations by allowing you to handle EOF conditions appropriately.
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