Concepts / File Modes: Reading, Writing, and Appending

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.

  • Programming

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.

Programrequests a file by nameOperating systemlocates and verifiesNamed fileexists and is readableFile handlereference toolfile-open requestlocates and verifiesreturns reference
What happens when a program requests a file by name?

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.

Open requestfile name suppliedFile locatedby nameFile existsFile readableFile handleopen connectionOpen does notcompleterequired condition failedsubmitlocatednot locatedyesnoyesno
Which conditions must be satisfied for the file-open request described in this lesson to succeed?
Worked example

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?

  1. 1
    Request

    The program asks to open the named file.

  2. 2
    Locate

    The operating system uses the file name to locate the file.

  3. 3
    Verify

    The operating system verifies that the file exists and is readable.

  4. 4
    Return

    The operating system returns a file handle, which is a reference tool for working with the open file.

Result

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.

File handlereference and connectionMetadataname, mode, encodingHandle methodsused to read contentsFile contentsactual stored datacontains access informationprovidesaccess
What is the relationship between the returned handle and the contents stored in the file?

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.

Readingaccess modeHandle metadataincludes modeWritingaccess modeAppendingaccess moderecorded for accessrecorded for accessrecorded for access
How do the three named modes differ in the access choice recorded with an open file?

Mistakes to Avoid

  • Common mistake

    Treating the file handle as if it were the file's contents.

    What it looks like

    A program opens a file and assumes the returned handle already contains the stored data.

    Why it fails

    The source distinguishes the handle, which is a reference tool, from the actual file data.

    Do this instead

    Use operations on the file handle to access the actual contents.

  • Common mistake

    Skipping the operating system's role.

    What it looks like

    Describing an open operation as though the program simply receives data by naming a file.

    Why it fails

    Opening asks the operating system to locate the file and verify that it exists and is readable.

    Do this instead

    Trace 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 like

    Treating the mode stored in the handle as if it were the data stored in the file.

    Why it fails

    Mode is metadata about access, while the contents are the actual stored data.

    Do this instead

    Keep access metadata and file data as separate concepts.

Check Your Understanding

EASY

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?

  • Yes, because locating the file is sufficient

  • No, because readability is also verified

  • Yes, because the handle contains the contents

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

  1. 1
    Opening a file asks the operating system to locate it by name and verify that it exists and is readable.
  2. 2
    A successful open returns a file handle, which is a reference tool and connection object.
  3. 3
    The handle is not the actual data stored in the file.
  4. 4
    The handle contains metadata such as the file name, access mode, and encoding.
  5. 5
    The 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.

More concepts starting with F