### close -- Close File Associated with Handle

Format

>     #include <io.h>
>     int close(int handle);

Language Level: XPG4, Extension
close closes the file associated with the handle. Use close if you opened the handle with open. If you opened the handle with fopen, use fclose to close it.

Note: In earlier releases of the C/C++ run-time library, close began with an underscore (_close). Because it is defined by the X/Open standard, the underscore has been removed. For compatibility, IBM C and C++ Compilers will map _close to close for you.

Return Value
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.

Example
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.
       **************************************************************************/
    }

fclose -- Close Stream
creat -- Create New File
open -- Open File
_sopen -- Open Shared File
<io.h>