C – EOF – Close File

EOF (End Of File):

  • It is pre-defined constant variable.
  • EOF is a macro(constant)
  • Constant variables represent in capital letters(in all programming languages)
  • EOF is assigned with value -1
  • EOF represents “End Of File” while processing file data.
#include<stdio.h>
int main()
{
            printf(“EOF value is : %d \n”, EOF);
            printf(“EOF character is : %c \n”, EOF);
            return 0;
}

Closing the File: fclose() is used to close the file after use.

  • It is recommended to release every resource(file) after use.
  • On success, it returns 0.
  • On Failure, it returns -1.
    • int  fclose(FILE* stream);
We must release Resource (File, Database) after use. Improper shutdown causes loss of data.
#include<stdio.h>
int main()
{
            FILE* p;
            int r1, r2;
            p = fopen(“code.c”, “r”);
            if(p != NULL){
                        r1 = fclose(p);
                        printf(“On success : %d \n”, r1);
                        r2 = fclose(p);
                        printf(“On failure : %d \n”, r2);
            }
            return 0;
}
Scroll to Top