A stream of type ifstream
can be connected to a file with a call to the member function open
. Your program can then take input from that file.
A stream of type ofstream
can be connected to a file with a call to the member function open
. Your program can then send output to that file.
You should use the member function fail
to check whether a call to open
was successful.
An object is a variable that has functions associated with it. These functions are called member functions. A class is a type whose variables are objects. A stream is an example of an object. The types ifstream
and ofstream
are examples of classes.
The following is the syntax you use when you write a call to a member function of an object:
Calling_Object.Member_Function_Name(Argument_List);
An example with the stream cout
as the calling object and precision
as the member function is the following:
cout.precision(2);
Stream member functions, such as width
, setf
, and precision
, can be used to format output. These output functions work the same for the stream cout
, which is connected to the screen, and for output streams connected to files.
Every input stream has a member function named get
that can be used to read one character of input. The member function get
does not skip over whitespace. Every output stream also has a member function named put
that can be used to write one character to the output stream.
The member function eof
can be used to test for when a program has reached the end of an input file. The member function eof
works well for text processing. However, when processing numeric data, you might prefer to test for the end of a file by using the other method we discussed in this chapter.
A function may have formal parameters of a stream type, but they must be call-by-reference parameters; they cannot be call-by-value parameters. The type ifstream
can be used for an input-file stream, and the type ofstream
can be used for an output-file stream. (See the next summary point for other type possibilities.)
If you use istream
(spelled without the f
) as the type for an input-stream parameter, then the argument corresponding to that formal parameter can be either the stream cin
or an input-file stream of type ifstream
(spelled with the f
). If you use ostream
(spelled without the f
) as the type for an output stream parameter, then the argument corresponding to that formal parameter can be either the stream cout
or an output-file stream of type ofstream
(spelled with the f
).