gtpc2m0wC/C++ Language Support User's Guide

close-Close a File

This function closes a file.

Note

This description applies only to files. See TPF Transmission Control Protocol/Internet Protocol for more information about the close function for sockets.

Format

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

fildes
The file descriptor of the open file that is to be closed.

This function closes file descriptor fildes. This frees the file descriptor to be returned by future open calls and other calls that create file descriptors.

When the last open file descriptor for a file is closed, the file itself is closed.

The close function unlocks (removes) all outstanding record locks that the process has on the associated file.

TPF deviation from POSIX

If the file was opened with write access or with read and write access, close updates the st_mtime field with the current time.

Normal Return

If successful, the close function returns 0.

Error Return

If unsuccessful, the close function returns -1 and sets errno to the following:

EBADF
fildes is not a valid open file descriptor.

Programming Considerations

None.

Examples

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
 
main() {
  int fd;
  char out[20]="Test string";
  if ((fd = creat("my.file")) < 0)
    perror("creat error");
  else {
    if (write(fd, out, strlen(out)+1) == -1) {
      perror("write() error");
    if (fd = 0) perror("close() error");
    close(fd);
  }
}

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.