C EOF & fclose() function : <stdio.h>



EOF and feof function >> stdio.h >> File Handling in C

Syntax :
int feof(FILE *stream);

What it does ?
  1. Macro tests if end-of-file has been reached on a stream.
  2. feof is a macro that tests the given stream for an end-of-file indicator.
  3. Once the indicator is set, read operations on the file return the indicator until rewind is called, or the file is closed.
  4. The end-of-file indicator is reset with each input operation.
Ways of Detecting End of File :
A ]   In Text File :
  1. Special Character EOF denotes the end of File
  2. As soon as Character is read,End of the File can be detected
  3. EOF is defined in stdio.h
  4. Equivalent value of EOF is  -1
Printing Value of EOF :
void main()
{
printf("%d", EOF);
}
B ]   In Binary File :
  1. feof  function is used to detect the end of file
  2. It can be used in text file
  3. feof Returns TRUE if end of file is reached

Syntax :

int feof(FILE *fp);
Ways of Writing feof Function :  
Way 1 : In if statement :  
if( feof(fptr) == 1 ) // as if(1) is TRUE
printf("End of File");
Way 2 : In While Loop
while(!feof(fptr))
{
--- - --
--- - --
}

Bookmark & Share