18.3 Input Streams

An input stream object is a source of bytes. Refer to the class hierarchy diagram in the Class Libraries Reference. The three most important input stream classes are istream, ifstream, and istrstream as described below.

The istream Class

The istream class is best suited to sequential text-mode input. Objects of class istream can be configured for buffered or unbuffered operation. All the functionality of the base class, ios, is included in istream. See the Class Libraries Reference for details.

Rarely will you construct an object from class istream. You will generally use the predefined cin object (actually an object of class istream_withassign). Under certain circumstances, you may assign cin to other stream objects after program startup.

All the functionality of the base class, ios, is included in istream. For details, see Part 3 of the Class Libraries Reference.

The ifstream Class

The ifstream class supports disk file input. Construct an object of class ifstream if you need an input-only disk file. You can specify whether ifstream objects process binary or text-mode data.

If you specify a filename in the constructor, that file is automatically opened when the object is constructed. Otherwise you can use the open member function after invoking the void-argument constructor.

Many formatting options and member functions apply to ifstream objects. All the functionality of the base classes ios and istream is included in ifstream, and that includes the ability to open files in either buffered or unbufferd mode. See Part 3 of the Class Libraries Reference for details.

The istrstreamClass

The istrstream class supports input from in-memory strings in a manner similar to the C run-time library function sscanf. Construct an object of class istrstream when you need to extract data from an existing null-terminated character array (C string). You must have allocated and initialized your string before calling the constructor.