_close

Closes a file.

int _close( int handle );

Routine Required Header Optional Headers Compatibility
_close <io.h> <errno.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

_close returns 0 if the file was successfully closed. A return value of –1 indicates an error, in which case errno is set to EBADF, indicating an invalid file-handle parameter.

Parameter

handle

Handle referring to open file

Remarks

The _close function closes the file associated with handle.

Example

/* OPEN.C: This program uses _open to open a file
 * named OPEN.C for input and a file named OPEN.OUT
 * for output. The files are then closed.
 */

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>

void main( void )
{
   int fh1, fh2;

   fh1 = _open( "OPEN.C", _O_RDONLY );
   if( fh1 == -1 )
      perror( "open failed on input file" );
   else
   {
      printf( "open succeeded on input file\n" );
      _close( fh1 );
   }

   fh2 = _open( "OPEN.OUT", _O_WRONLY | _O_CREAT, _S_IREAD | 
                            _S_IWRITE );
   if( fh2 == -1 )
      perror( "Open failed on output file" );
   else
   {
      printf( "Open succeeded on output file\n" );
      _close( fh2 );
   }
}

Output

Open succeeded on input file
Open succeeded on output file
 

Low-Level I/O Routines

See Also   _chsize, _creat, _dup, _open, _unlink