[Chapter 10] 10.2 The close() Function
Author: Dale Dougherty & Arnold Robbins
Date: 1998-08-03T21:01:39Z

#

| Chapter 10 The Bottom Drawer |

## 10.2 The close() Function

The `close()` function allows you to close open files and pipes. There are a number of reasons you should use it.

- You can only have so many pipes open at a time. (See the section "Limitations" below, which describes how such limitations can differ from system to system.) In order to open as many pipes in a program as you wish, you must use the `close()` function to close a pipe when you are done with it (ordinarily, when `getline` returns 0 or -1). It takes a single argument, the same expression used to create the pipe. Here's an example:

  >     close("who")

- Closing a pipe allows you to run the same command twice. For example, you can use `date` twice to time a command.

- Using `close()` may be necessary in order to get an output pipe to finish its work. For example:

  >     { some processing of $0 | "sort > tmpfile" }
  >     END {
  >     	close("sort > tmpfile")
  >     	while ((getline < "tmpfile") > 0) {
  >     		do more work
  >     	}
  >     }

- Closing open files is necessary to keep you from exceeding your system's limit on simultaneously open files.

We will see an example of the `close()` function in the section "Working with Multiple Files" later in this chapter.

10.1 The getline Function |  | 10.3 The system() Function