Contents
Section 8.2 File Input and Output
The C++ language does not deal directly with input and output. Instead, IO is handled by a family of types defined in the standard library. These types support IO to and from devices such as files and console windows. Additional types allow in-memory IO to and from string
s.
The IO library defines operations to read and write values of the built-in types. In addition, classes, such as string
, typically define similar IO operations to work on objects of their class type as well.
This chapter introduces the fundamentals of the IO library. Later chapters will cover additional capabilities: Chapter 14 will look at how we can write our own input and output operators, and Chapter 17 will cover how to control formatting and how to perform random access on files.
Our programs have already used many IO library facilities. Indeed, we introduced most of these facilities in § 1.2 (p. 5):
• istream
(input stream) type, which provides input operations
• ostream
(output stream) type, which provides output operations
• cin
, an istream
object that reads the standard input
• cout
, an ostream
object that writes to the standard output
• cerr
, an ostream
object, typically used for program error messages, that writes to the standard error
• The >>
operator, which is used to read input from an istream
object
• The <<
operator, which is used to write output to an ostream
object
• The getline
function (§ 3.2.2, p. 87), which reads a line of input from a given istream
into a given string