Closing Files and Resource Management
Opening a file asks the operating system to locate the file by name and verify it exists and is readable.
The Request Behind an Open
Opening a file is not the same as immediately receiving all of its contents. It is a request that involves the operating system. The request identifies a file by name, and the operating system checks whether the file exists and is readable. If those conditions are satisfied, the operating system returns a file handle that the program can use to work with the file.
Tracing the Open Operation
The open operation can be understood as a short sequence. First, the program supplies a file name. Next, the operating system attempts to locate that file. It then verifies that the file exists and is readable. A successful result is not the file's text or other stored data. The successful result is a file handle, which gives the program a way to work with the open file.
Handle and Contents
A file handle is a reference tool or connection object, not the actual data stored in the file. It contains metadata about the program's access, including the file name, mode, and encoding. The contents remain in the file. To read those contents, the program uses methods on the file handle.
A Handle Is Not a Text File
Imagine that a program successfully opens a file named notes.txt. What does it receive, and how would it obtain the file's stored text?
Locate and verify: The operating system locates notes.txt and verifies that it exists and is readable.
Return the handle: The operating system returns a file handle. This handle is a connection object, not the text inside notes.txt.
Use the handle: The program uses methods on the handle to access the actual contents.
The handle provides the route to the contents. It is not a copy of the contents themselves.
Closing as Resource Management
Closing a file belongs to the resource-management part of working with files. The program should treat the file handle as a connection that has a beginning and an end: it is obtained after a successful open and should no longer be used once the program has finished working with the file. The provided material explains what opening returns and how the handle provides access; it does not specify implementation-specific details about how an operating system releases resources after closing.
Common Handle Mistakes
Treating the file handle as if it were the file's contents.
The handle is a reference or connection object. The actual contents must be accessed through methods on the handle.
Fix:
Separate the access tool from the data: the handle provides access, while the file contains the data.Assuming that naming a file guarantees a successful open.
A successful open requires the file to be located and verified as readable.
Fix:
Think of opening as a request followed by existence and readability checks.Assuming that a file handle contains no useful information.
The handle is a connection object containing metadata about access, including the file name, mode, and encoding.
Fix:
Recognize the handle as both an access tool and a source of file-access metadata.
Check Your Model
A program requests a file by name. The operating system confirms that the file exists and is readable, then returns a file handle. Identify which item is the access tool, which item is the actual data, and name three kinds of metadata associated with the handle.
Hints
- The access tool is returned by the operating system after the checks succeed.
- The actual data remains in the file rather than being identical to the handle.
- The source identifies the file name, mode, and encoding as handle metadata.
What do you think happens?
What does a successful file open return: the file's contents or a file handle?
Reveal answer
Answer: A file handle
The operating system returns a file handle, which is a reference or connection object. The program uses methods on that handle to access the actual file contents.
Essential Takeaways
- Opening a file asks the operating system to locate it by name and verify that it exists and is readable.
- A successful open returns a file handle, not the file's actual data.
- A file handle is a connection object and reference tool containing metadata such as the file name, mode, and encoding.
- The program accesses file contents by using methods on the file handle.
- Closing is the deliberate end of the program's use of the file handle and is part of responsible resource management.
Key Takeaways
- Opening a file involves the operating system locating the named file and checking that it exists and is readable.
- The result of a successful open is a file handle rather than the file's contents.
- The handle provides access to the file and contains metadata such as its name, mode, and encoding.
- File contents are accessed through methods on the handle.
- Closing ends the program's use of the handle and fits within the broader idea of resource management.