File input and output (I/O) aren't glamorous parts of computer programming, but they are fundamental and therefore important. Contemporary C++ programs replace many file I/O operations with database or GUI operations. Nevertheless, these high-level operations rest on C++'s lower-level I/O operations and on an OS's low-level operations. Without file I/O operations, programs can only process and produce limited amounts of data. The large number of C++ API classes and functions, coupled with their numerous options, exacerbates the situation, making learning how to use language-specific documentation essential.
The C++ API also provides classes combining the behaviors of I/O streams and strings (C-strings and instances of the string class). These string streams support advanced data input validation, data type conversions, data extraction or parsing operations, and formatting and accumulating output. Programmers frequently use regular expressions (RE) to validate user input, either through the console or GUI features. The chapter introduces and demonstrates string streams and regular expressions at the end.
It's convenient to think of data moving between a file and a program as a stream or sequence of bytes. Hence, the C++ objects that perform I/O operations are called stream objects or simply streams. We'll organize the C++ I/O features in four ways to help manage their complexity:
Stream names. The stream class names typically include one or more characters that signal their use or behavior. For example, the "c" in cin and cout indicate that they read from and write to the console. Additionally, "if" and "of" signify file input and output, while "f" by itself signifies that the stream can read and write.
File type. Roughly speaking, files contain either textual or binary data. The following sections explore some of the specialized ways programs use both kinds of files. Opening a file as the correct type is essential for C++ programs running on Windows systems.
Transfer units. C++ programs can transfer data, read or write it, as single characters, lines, or blocks, depending on the problem the program solves.
Transfer order. C++ programs typically access file data sequentially (from the beginning to the end in order) or randomly. "Random" means that the order isn't predetermined, but that outside factors determine the I/O access order. Direct access is a synonym for random access, suggesting that a program can directly go to a specific unit of file data. Direct file access is essential for database operations.
Know
How to represent files as stream objects
What the file position pointer is and does
How to open a file (stream) in text and binary mode
How to close a file (stream)
How to use the file status functions: good, bad, and fail
How to read/write a file
One character at a time
One line at a time
Know about less common features
The difference between sequential and random access
How to read/write a file one block at a time
Buffer I/O
the gcount function
Random access file pointer positioning functions: seekg, seekp, tellg, and tellp