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?
- 1Request
The program asks to open the named file.
- 2Locate
The operating system uses the file name to locate the file.
- 3Verify
The operating system verifies that the file exists and is readable.
- 4Return
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
Common mistake Treating the file handle as if it were the file's contents.
What it looks likeA program opens a file and assumes the returned handle already contains the stored data.
Why it failsThe source distinguishes the handle, which is a reference tool, from the actual file data.
Do this insteadUse operations on the file handle to access the actual contents.
Common mistake Skipping the operating system's role.
What it looks likeDescribing an open operation as though the program simply receives data by naming a file.
Why it failsOpening asks the operating system to locate the file and verify that it exists and is readable.
Do this insteadTrace the request through locating and verification before describing the returned handle.
Common mistake Assuming that a mode explains the file's contents.
What it looks likeTreating the mode stored in the handle as if it were the data stored in the file.
Why it failsMode is metadata about access, while the contents are the actual stored data.
Do this insteadKeep 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
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
- 1Opening a file asks the operating system to locate it by name and verify that it exists and is readable.
- 2A successful open returns a file handle, which is a reference tool and connection object.
- 3The handle is not the actual data stored in the file.
- 4The handle contains metadata such as the file name, access mode, and encoding.
- 5The actual file contents are accessed through operations provided by the file handle.
At a glance
- 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.