File Modes: Reading, Writing, and Appending
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
When a program opens a file, it is not immediately holding the file's contents. It is making a request to the operating system. The request identifies a file by name and asks the operating system to locate it and verify that it exists and is readable.
The Open Operation
The operating system has an important role in opening a file. It must locate the file using the name supplied by the program. It must then verify the conditions described by this topic: that the file exists and is readable. If those conditions are satisfied, the operating system returns a file handle. The handle represents the program's connection to the open file; it is not a copy of the file's contents.
A Successful Open Request
A program requests a file by name. The operating system can locate that file, and the file is readable. What does the program receive?
Request: The program asks to open the named file.
Locate: The operating system uses the file name to locate the file.
Verify: The operating system verifies that the file exists and is readable.
Return: The operating system returns a file handle, which is a reference tool for working with the open file.
The program receives a file handle, not the file's actual contents.
Handle and Contents
A file handle and file contents are different things. The file contents are the data stored in the file. The file handle is the reference tool returned after the operating system opens the file. To obtain the actual contents, the program must use operations provided by the file handle. The handle is therefore the key used to access the file, not the data stored inside it.
Access Modes
Reading, writing, and appending are the three access modes named in this topic. A file handle contains metadata about the access to the file, including the file name, the mode, and the encoding. The mode is therefore part of the handle's description of how the program is accessing the open file.
Mistakes to Avoid
Treating the file handle as if it were the file's contents.
The source distinguishes the handle, which is a reference tool, from the actual file data.
Fix:
Use operations on the file handle to access the actual contents.Skipping the operating system's role.
Opening asks the operating system to locate the file and verify that it exists and is readable.
Fix:
Trace the request through locating and verification before describing the returned handle.Assuming that a mode explains the file's contents.
Mode is metadata about access, while the contents are the actual stored data.
Fix:
Keep access metadata and file data as separate concepts.
Check Your Understanding
A program requests a file by name. The operating system locates the file and verifies that it exists and is readable. Explain what the program receives, what that returned object represents, and how the program obtains the actual file contents.
Hints
- Separate the returned reference from the stored data.
- Name the operating system's two checks.
- Think about what the handle provides access to.
What do you think happens?
A file-open request can locate a named file, but the file is not readable. Does the described process reach the successful-open result?
Reveal answer
Answer: No, because readability is also verified.
The source describes a successful open as requiring the operating system to locate the file and verify that it exists and is readable.
Key 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, which is a reference tool and connection object.
- The handle is not the actual data stored in the file.
- The handle contains metadata such as the file name, access mode, and encoding.
- The actual file contents are accessed through operations provided by the file handle.
Key Takeaways
- Opening a file involves the operating system locating the file and verifying that it exists and is readable.
- The result of a successful open is a file handle, not the file's contents.
- A file handle provides a connection and metadata about access, including the file name, mode, and encoding.
- Programs use operations on the handle to access the actual stored data.