Chapter 16. File Input/Output
I am the heir of all the ages, in the foremost files of time.
A file is a collection of related data. C++ treats a file as a series of bytes. Many files reside on disk; however, devices such as printers, magnetic tapes, and communication lines are also considered files.
This chapter discusses three different I/O packages. The first is the C++ I/O stream classes. This is the most commonly used I/O system and the one we’ve been using up to now. Next, we examine the raw I/O routines that give us direct access to the low-level I/O. Finally we look at the C I/O system. Although it is somewhat outdated, C I/O calls still appear in old code. Also, in some cases, the C-style I/O routines are superior to the ones provided with C++.
C++ File I/O
C++ file I/O is based on three classes: the istream class for
input, the ostream class for
output, and the iostream class for
input/output. C++ refers to files as streams
since it considers them a stream of bytes. Four class variables are
automatically created when you start a program. These are listed in
Table 16-1.
Table 16-1. Predefined I/O class variables
Variable | Use |
|---|---|
| Console input (standard input) |
| Console output (standard output) |
| Console error (standard error) |
| Console log |
These variables are defined in the standard include file
<iostream> . Normally, std::cin
is assigned to the keyboard and std::cout,
std::cerr, and std::clog are assigned to the screen. Most operating systems allow you ...