EOF, feof() and ferror() Functions in C Language with Examples

End of File (EOF) in C Language

In C language, EOF stands for End of File. It is a macro defined in the standard input/output header file stdio.h. It is returned by input functions such as fgetc() when the end of a file is encountered or a reading error occurs. EOF is not an actual character stored at the end of a file. It is a special negative integer value used to indicate the end of input or a reading failure.

सी लैंग्वेज में EOF का पूरा नाम End of File है। यह Standard Input/Output Header File stdio.h में Defined एक Macro है। fgetc() जैसे Input Functions File के अंत में या Reading Error होने पर EOF Return करते हैं। EOF वास्तव में File के अंत में Store किया गया कोई Character नहीं है। यह एक Special Negative Integer Value है, जिसका उपयोग Input के समाप्त होने या Reading Failure को दर्शाने के लिए किया जाता है।

Example:

int ch;
ch = fgetc(fp);
if (ch == EOF)
{
printf("End of file or reading error.");
}

The variable ch is declared as int because fgetc() must be able to return every possible unsigned character value as well as the special value EOF.

यहां ch Variable को int Declare किया गया है क्योंकि fgetc() को सभी Possible Character Values के साथ-साथ Special Value EOF भी Return करने में सक्षम होना चाहिए।

1. feof() Function in C

The feof() function is used to check whether the end-of-file indicator of a file stream is set. It returns a non-zero value if the end-of-file indicator is set; otherwise, it returns zero.

feof() Function का उपयोग यह Check करने के लिए किया जाता है कि File Stream का End-of-File Indicator Set हुआ है या नहीं। यदि Indicator Set होता है, तो यह Non-Zero Value Return करता है, अन्यथा Zero Return करता है।

Syntax:

int feof(FILE *stream);

Example:

if (feof(fp))
{
printf("End of file reached.");
}

This statement checks whether the end-of-file indicator of the file stream is set.

यह Statement Check करता है कि File Stream का End-of-File Indicator Set हुआ है या नहीं।

Return Value:

  • Non-zero value – End-of-file indicator is set.

  • Zero – End-of-file indicator is not set.

  • Non-Zero Value – End-of-File Indicator Set है।

  • Zero – End-of-File Indicator Set नहीं है।

Important Note: The feof() function does not check whether the next character will be the last character. The end-of-file indicator is set only after an input operation attempts to read beyond the available data.

feof() Function यह नहीं बताता कि अगला Character File का अंतिम Character होगा या नहीं। यह Indicator तभी Set होता है जब Input Operation उपलब्ध डेटा के आगे Read करने का प्रयास करता है।

2. ferror() Function in C

The ferror() function is used to check whether an error has occurred during an input or output operation on a file stream. It returns a non-zero value if the error indicator is set; otherwise, it returns zero.

ferror() Function का उपयोग File Stream पर Input या Output Operation के दौरान Error हुआ है या नहीं, यह Check करने के लिए किया जाता है। यदि Error Indicator Set होता है, तो यह Non-Zero Value Return करता है, अन्यथा Zero Return करता है।

Syntax:

int ferror(FILE *stream);

Example:

if (ferror(fp))
{
printf("File reading error.");
}

This statement checks whether the error indicator of the specified file stream is set.

यह Statement Check करता है कि दी गई File Stream का Error Indicator Set हुआ है या नहीं।

Return Value:

  • Non-zero value – Error indicator is set.

  • Zero – Error indicator is not set.

  • Non-Zero Value – Error Indicator Set है।

  • Zero – Error Indicator Set नहीं है।

Difference Between EOF, feof() and ferror()

EOFfeof()ferror()
A macro defined in stdio.h.A standard library function.A standard library function.
Indicates end of input through a return value.Checks the end-of-file indicator.Checks the error indicator.
Commonly compared with the return value of fgetc().Returns non-zero if the EOF indicator is set.Returns non-zero if the error indicator is set.
It is not a function.Syntax: feof(fp)Syntax: ferror(fp)

EOF एक Macro है, जबकि feof() और ferror() Functions हैं। EOF Input के समाप्त होने या Reading Failure को Return Value के माध्यम से दर्शाता है, feof() End-of-File Indicator और ferror() Error Indicator को Check करता है।

C Program to Demonstrate EOF and feof()

The following program reads the contents of a text file named new.txt character by character using fgetc(). It displays the contents and checks whether the end-of-file indicator is set.

निम्नलिखित प्रोग्राम fgetc() Function की सहायता से new.txt नाम की Text File को एक-एक Character पढ़ता है। यह File के Contents को Display करता है और Check करता है कि End-of-File Indicator Set हुआ है या नहीं।

Program:

#include <stdio.h>
int main(void)
{
FILE *fp;
int ch;
fp = fopen("new.txt", "r");
if (fp == NULL)
{
printf("Cannot open file.\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF)
{
putchar(ch);
}
if (ferror(fp))
{
printf("\nError while reading the file.\n");
}
else if (feof(fp))
{
printf("\nReached the end of file.\n");
}
fclose(fp);
return 0;
}

Input File: new.txt

Create a text file named new.txt and write the following contents in it.

new.txt नाम की एक Text File बनाएं और उसमें निम्नलिखित Contents लिखें।

C Programming
File Handling in C

Output

C Programming
File Handling in C
Reached the end of file.

Explanation

  1. fopen("new.txt", "r") opens the specified file in read mode. यह दी गई File को Read Mode में Open करता है।

  2. fgetc(fp) reads one character at a time from the file. यह File से एक बार में एक Character Read करता है।

  3. while ((ch = fgetc(fp)) != EOF) continues reading until the input function returns EOF. यह Loop तब तक चलता है जब तक Input Function EOF Return नहीं करता।

  4. putchar(ch) displays the character on the screen. यह Read किए गए Character को Screen पर Display करता है।

  5. ferror(fp) checks whether a reading error occurred. यह Check करता है कि File Reading के दौरान कोई Error हुआ है या नहीं।

  6. feof(fp) checks whether the end-of-file indicator is set. यह Check करता है कि File का End-of-File Indicator Set हुआ है या नहीं।

  7. fclose(fp) closes the opened file. यह Open की गई File को Close करता है।

Difference Between feof() and ferror()

feof()ferror()
Checks the end-of-file indicator.Checks the error indicator.
Returns non-zero when the EOF indicator is set.Returns non-zero when the error indicator is set.
Used to identify an attempted read beyond available data.Used to identify an input or output error.

feof() और ferror() दोनों File Stream के Indicators को Check करते हैं। feof() File के अंत से संबंधित Indicator को तथा ferror() File Operation में हुई Error से संबंधित Indicator को Check करता है।

Important Points

  1. EOF is a macro, not a character or function. EOF एक Macro है, Character या Function नहीं।

  2. EOF is commonly returned by input functions such as fgetc() when the end of input is reached or a reading error occurs. fgetc() जैसे Input Functions Input समाप्त होने या Reading Error होने पर EOF Return कर सकते हैं।

  3. Always store the return value of fgetc() in an int variable. fgetc() का Return Value Store करने के लिए हमेशा int Variable का उपयोग करें।

  4. feof() should be checked after an input operation, not before attempting to read the file. feof() को File Read करने के प्रयास के बाद Check करना चाहिए, पहले नहीं।

  5. ferror() is used to check whether an input or output error has occurred. इसका उपयोग Input या Output Operation में Error Check करने के लिए किया जाता है।

  6. feof() and ferror() do not automatically close a file. File को Close करने के लिए fclose() Function का उपयोग करना आवश्यक है।

Note: In standard C, there is no standard library function named eof(). Use feof() to check the end-of-file indicator.

Standard C में eof() नाम का कोई Standard Library Function नहीं है। End-of-File Indicator को Check करने के लिए feof() Function का उपयोग करें।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025