Chapter Six Files and Stream
Chapter Six Files and Stream
3. Perform input/output operations on the stream, via the functions defined in the
stream's pubic interface in a device independent manner.
❖ Some functions convert the data between the external format and internal format
(formatted IO); while other does not (unformatted or binary IO).
4. Disconnect (Dissociate) the stream to the actual IO device (e.g., close the file).
5. Free the stream object.
File Input/output (Header <fstream>)
• C++ handles file IO similar to standard IO.
• C++ provides the following classes to perform output and input to/from files:
• ofstream: Stream class to write on files
• ifstream: Stream class to read from files
• fstream: Stream class to both read and write from/to files.
• These classes are derived directly or indirectly from the classes istream and ostream. We have already
used objects whose types were these classes: cin is an object of class istream and cout is an object of
class ostream.
• In header:
• The class ofstream is a subclass of ostream;
• ifstream is a subclass of istream; and
• fstream is a subclass of iostream for bidirectional IO.
• To write to a file, you construct an ofsteam object connecting to the output file, and use the ostream
functions such as stream insertion <>, get(), getline() and read().
• File IO requires an additional step to connect the file to the stream (i.e., file open) and disconnect from
the stream (i.e., file close).
OPENING AND CLOSING A FILE
• A file can be opened in C++ by two methods:
1. By using the constructor of the stream class to be used
2. By using the open( ) function of the stream class to be used
• For reading entire lines of text :
• C++ provides get( ) and getline( ) functions as input member functions of the ifstream
class.
• It also provides a put( ) function as output member function of the ofstream class.
• In addition to text files, we can also read and write binary files.
• The major advantage of binary files is that they require less memory space for storage of data.
• Moreover, these files can be used to read or write structured data such as structures, class
objects etc.
• When we are finished with our input and output operations on a file, we shall close it so
that the operating system is notified and its resources become available again.
• Syntax: myfile.close();
File Output
❑ The steps are:
1. Construct an ostream object.
2. Connect it to a file (i.e., file open) and set the mode of file operation (e.g, truncate,
append).
3. Perform output operation via insertion >> operator or write(), put() functions.
4. Disconnect (close the file which flushes the output buffer) and free the ostream
object.
#include<fstream> ❖ By default, opening an output file
….. creates a new file if the filename does
ofstream fout; // create ostream object not exist; or truncates it (clear its
fout.open(filename, mode);// open file content) and starts writing as an
….. empty file.
fout.close();// close file
• You can set multiple flags via bit-or (|) operator, e.g., ios::out | ios::app to append output at the end
of the file.
• For output, the default is ios::out | ios::trunc. For input, the default is ios::in
File Input
• The steps are:
1. Construct an istream object.
2. Connect it to a file (i.e., file open) and set the mode of file operation.
3. Perform output operation via extraction << operator or read(), get(), getline()
functions.
4. Disconnect (close the file) and free the istream object.
#include<fstream>
ifstream fin;
fin.open(filename, mode);
......
fin.close();
// OR combine declaration and open()
ifstream fin(filename, mode);
File Input
• Example: