Syntax
#include <io.h> int close(int handle);Description
close returns 0 if it successfully closes the file. A return value of -1 shows an error, and close sets errno to EBADF, showing an incorrect file handle argument.
This example opens the file edclose.dat and then closes it using the close function.
#include <io.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <stdlib.h>
int main(void)
{
   int fh;
   printf("\nCreating edclose.dat.\n");
   if (-1 == (fh = open("edclose.dat", O_RDWR|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE)
      )) {
      perror("Unable to open edclose.dat");
      return EXIT_FAILURE;
   }
   printf("File was successfully opened.\n");
   if (-1 == close(fh)) {
      perror("Unable to close edclose.dat");
      return EXIT_FAILURE;
   }
   printf("File was successfully closed.\n");
   return 0;
   /****************************************************************************
      The output should be:
      Creating edclose.dat.
      File was successfully opened.
      File was successfully closed.
   ****************************************************************************/
}
Related Information