header

Input/Output

In C++, input and output is modeled as a stream. Text input and output is modeled as a stream of characters. Characters are inserted into a stream at one end and extracted at the other. An input stream is one in which characters are inserted by some external device and extracted by the application program. An output stream is one in which the characters are inserted by the application and extracted by some external device.

I/O Streams

The syntax for inserting something into an output stream is given here:

streamId << item;

where streamId is the stream identifier, "<<" is the insertion operator, and item is the item to be inserted into the stream. The item can be a literal value or a variable. If it is a variable, the stream will automatically convert its value into an appropriate text representation. You can insert additional items into the stream by preceding each item with the insertion operator.

The syntax for extracting something from an input stream is given here:

streamId >> item;

where streamId is the stream identifier or name, ">>" is the extraction operator, and item is the item to be extracted from the stream. The item must be a variable. The input stream automatically converts the incoming text into a value of the same type as the variable. If the text cannot be converted, the stream fails and no further input is possible. You can extract additional items from the stream by preceding each item with the extraction operator.

The items in the input stream are  whitespace delimited. That is, any leading whitespace characters that are extracted from the stream are discarded. Extraction then continues until a terminating whitespace character is encountered. This whitespace character is not extracted from the stream. Extraction will also terminate if an invalid character is encountered in the stream. For example, if the letter "p" is extracted while trying to read an integer value. In that case, the stream fails and further input is not possible.

Because the extraction operator is whitespace delimited, it can not be used to extract a string value that includes spaces (or any other whitespace). The getline function is used for this purpose. See the getline page for details.

Standard Input/Output

The standard input stream and the standard output stream are declared and initialized in the iostream include file. Conceptually, the standard input stream connects the keyboard to the application. That is, characters are inserted into the standard input stream by the keyboard and extracted by our program. The name of the standard input stream is "cin" which stands for "character input".

The standard output stream connects our program to the computer screen. Our program inserts characters into the standard output stream and those characters are extracted by the screen device and displayed. The name of the standard output stream is "cout" which stands for "character output".

File Input/Output

File input/output also uses the stream model. However, there are no predefined standard streams for file input or file output. We have to declare and initialize our own stream variables. To use file streams you must include the fstream file in your program:

#include <fstream>

Declaring a File Stream Variable

There are two types of file streams. The data type for an input file stream is ifstream. Data from an associated text file is inserted by the operating system into an ifstream object. The application program extracts the data from an ifstream object using the extraction operator.

The second type of file stream is an output file stream and the corresponding data type is ofstream. Data is inserted into an output file stream by the application program using the insertion operator. The operating system extracts the data and writes it to a text file on a secondary memory device.

The declaration of a file stream variable looks like any other declaration; a data type followed by a variable identifier:

ifstream varId;  // An input file stream
ofstream varId;  // An output file stream

Opening a File

Conceptually, declaring a file stream variable connects or attaches the appropriate end of the stream to your application. The other end, however, is left dangling out in space (so to speak). Invoking the open method attaches the loose end of the stream to a specific file:

varId.open(fileName);

If varId is the name of an input file stream variable, the file whose name is fileName is opened for reading. If this file doesn't exist the input file stream fails.

If varId is the name of an output file stream variable, the file whose name is fileName is opened for writing. If the file does not exist, it is created as a new, empty, file. If it does already exist, its contents are erased and replaced by the data written to the file by the application program.

Accessing a File

The only thing you can do with an input file stream is extract data using the extraction operator. The statement

ifstreamId >> item;

extracts the specified item from the input file stream whose name is given by ifstreamId.

Similarly, the only thing you can do with an output file stream is insert data using the insertion operator. The statement

ofstreamId << item;

inserts the specified item into the output file stream whose name is given by ofstreamId.

Closing a File

When you are done using a file, you should close it. It's like going to somebody's house and opening their refrigerator. Whether you are putting something in or taking something out, it is only common courtesy to close the door when you are done. Closing a file breaks the connection between a specific text file and the corresponding stream. Here is the syntax for closing a file:

fstreamId.close();

The same syntax is used for both input file streams and output file streams. Notice that the empty parentheses following the word "close" are required and omitting them is a syntax error.