close

From RAD Studio
Jump to: navigation, search

Go Up to io.h Index


Header File

io.h

Category

Input/output Routines

Prototype

int close(int handle);

Description

Closes a file.

The close function closes the file associated with handle, a file handle obtained from a call to creat, creatnew, creattemp, dup, dup2, open, _rtl_creat, or _rtl_open.

It does not write a Ctrl-Z character at the end of the file. If you want to terminate the file with a Ctrl-Z, you must explicitly output one.

Return Value

Upon successful completion, close returns 0. On error (if it fails because handle is not the handle of a valid, open file), close returns a value of -1 and the global variable errno is set to

EBADF

Bad file number

Example

#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

main()
{
  int handle;
  char buf[11] = "0123456789";

  /* create a file containing 10 bytes */
  handle = open("NEW.FIL", O_CREAT);
  if (handle > -1)
  {
    write(handle, buf, strlen(buf));

    close(handle);                    /* close the file */
  }
  else
  {
    printf("Error opening file\n");
  }
  return 0;
}

Portability

POSIX Win32 ANSI C ANSI C++

+

+