Table of Contents
EOF, Getc() And Feof() In C
In C/C++, the getc()
function is used to read a character from a file, and it returns the End of File (EOF) value when the end of the file is reached or if it encounters an error. However, relying solely on comparing the return value of getc()
with EOF may not be sufficient to accurately determine if the end of the file has been reached. To address this issue, C provides the feof()
function. The feof()
function returns a non-zero value only when the end of the file has been reached, indicating that there are no more characters to read. In contrast, it returns 0 if the end of the file has not been reached. By using feof()
in conjunction with getc()
, developers can more reliably determine whether they have reached the end of a file while handling potential error conditions.
getc() Function in C
The getc()
function in C is commonly used to read a single character from the specified file stream. It is implemented as a macro in the <stdio.h>
header file. The getc()
macro takes a file pointer as an argument, reads the next character from the associated stream, and returns it as an unsigned char cast to an int. If the end of the file is encountered or an error occurs, it returns the special value EOF (End of File). This function is often used in conjunction with other file input/output functions to process data from files in C programming. It’s important to note that getc()
is essentially equivalent to the fgetc()
function, and in most implementations, getc()
is defined as a macro that expands to a call to fgetc()
.
Syntax of getc()
int getc(FILE* stream);
Parameters
- stream: It is referrred as a pointer to a file stream to read the data from.
Return Value
The getc()
function in C returns the character read from the file stream as an unsigned char cast to an int. If no error occurs and the End-Of-File (EOF) is not reached, it returns the character read. However, in case of an error or when the End-Of-File is encountered, it returns the special value EOF
. This is a crucial aspect of handling file operations in C, allowing developers to check for the end of the file or errors during file reading. Thank you for highlighting this important point.
feof() Function in C
The feof()
function in C is employed to determine whether the file pointer associated with a stream has reached the end of the file. When called, feof()
returns a non-zero value if the end of the file has been reached, indicating that there are no more characters to read from the file. Conversely, if the end of the file has not been reached, it returns 0. This function is commonly used in file-handling scenarios to conditionally check whether further attempts to read from the file should be made based on the status of the file pointer. It’s a useful tool in avoiding attempting to read beyond the end of a file.
Syntax of feof()
int feof(FILE* stream);
Parameters
It will returns the character read from the file stream.
Return Value
Whereas, If the end-of-file indicator is set for the stream, then it means that the function will return as a non-zero value (usually 1). Otherwise, it returns 0.
Example of feof()
In the program, the getc()
function is used to read a character from a file, and its return value is first checked for equality with EOF
. This check helps determine if the end of the file has been reached or if an error occurred during reading. Subsequently, the program employs the feof()
function to further confirm the end-of-file condition. If feof()
returns a non-zero value, it signifies the actual end of the file, and the program prints “End of file reached.” On the other hand, if feof()
returns 0, indicating that the EOF
from getc()
was due to an error, the program prints “Something went wrong.” This two-step verification ensures accurate handling of end-of-file situations and proper error management during file reading in the C program.
// C program to demonstrate the use of getc(), feof() and
// EOF
#include <stdio.h>
int main()
{
// Open the file "test.txt" in read mode
FILE* fp = fopen("test.txt", "r");
// Read the first character from the file
int ch = getc(fp);
// Loop until the end of the file is
// reached
while (ch != EOF) {
/* display contents of file on screen */
putchar(ch);
// Read the next character from the file
ch = getc(fp);
}
// Check if the end-of-file indicator is
// set for the file
if (feof(fp))
printf("\n End of file reached.");
else
printf("\n Something went wrong.");
// Close the file
fclose(fp);
// Wait for a keypress
getchar();
return 0;
}
Consider the file “test.txt” has the following data:
Skillvertex
FAQ- EOF, getc() and feof() in C
Q1. What is the use of feof () function in C?
Ans. The feof()
function in C indicates whether the end-of-file flag is set for a specific stream. This flag is raised by certain functions when the end of the file is reached. To reset or clear this flag, functions like rewind()
, fsetpos()
, fseek()
, or clearerr()
can be used on the respective stream. These operations are essential for managing file-related operations and ensuring accurate file handling in C programs.
Q2.What is the difference between EOF and feof functions in C?
Ans. In short, EOF
is a constant used to compare against return values (e.g., from getc()
) to check for the end of a stream. On the other hand, feof()
is a function that directly checks if a file pointer has reached the end of the associated file.
Q3.What is Getc () in C?
Ans. The getc()
function in C is utilized to read a single character from a specified file stream. Implemented as a macro in the <stdio.h>
header file, it plays a crucial role in file input operations in C programming.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together