Name

close - closes a file descriptor

Library

libc.lib

Synopsis

  #include <unistd.h>
  int close (int d);

Return values

The close() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.

Detailed description

The close system call deletes a descriptor from the per-process object reference table. If this is the last reference to the underlying object, the object will be deactivated. For example, on the last close of a file the current seek pointer associated with the file is lost; on the last close of a socket any file descriptor for that file is closed by that process.

When a process exits, all associated file descriptors are freed, but since there is a limit on active descriptors per processes, the close system call is useful when a large quantity of file descriptors are being handled.


Examples

/********************************************************************************************
* Detailed description   : This test code demonstrates usage of close  system call
*
* Preconditions :  None.
*******************************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
int main()
{
  int fd = 0;
  fd = open("Example.txt" , O_CREAT | O_RDWR , 0666);
  if(fd < 0 ) 
  {
    printf("Failed to open file Example.txt \n");
    return -1;
  }
 if(close(fd) < 0 )
 {
   printf("Failed to close  file Example.txt \n");
   return -1;
 }
 printf("File Example.txt closed \n" );
 return 0;
}

         

Output

File Example.txt closed

         


Errors

The close system call will fail if:
[EBADF]
  The d argument is not an active descriptor.
[EINTR]
  An interrupt was received(Not supported).
[ENOSPC]
  The underlying object did not fit, cached data was lost(Not supported).

See also

accept, fcntl, flock, open, pipe, socket

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top