To create a stream, a filename, described in the following section, and an I/O mode must be specified.
There are three general I/O modes that are specified with character strings, as follows:
- r: Opens an existing file for reading. It fails if the filename does not exist.
- w: Opens a file for writing. If the file exists, existing data is lost; otherwise, the file is created.
- a: Opens a file for appending. If the file exists, writing commences at the end of the file; otherwise, the file is created.
These are all one-way modes. That is, a file opened for reading with r can only be read; it cannot be updated. Two-way modes exist by appending + to each of the preceding modes—for example, r+, w+, and a+. When files are opened for reading and writing, care must be exercised to re-position the current file position so as not to inadvertently overwrite existing data. We will look at the file...