Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 10. The Bottom Drawer > The close( ) Function

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 Section 10.8 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.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint