Module Five
Module Five
FSTREAM OSTREAM
ISTREAM BASE
IOSTREAM
IFSTREAM FSTREAM
OFSTREAM
• Some of these classes we have worked with earlier such as:
• The extraction operator >> is a member of the istream class, and the insertion
operator << is a member of the ostream class.
• Both of these classes are derived from the ios class.
• The classes used for input and output to the video display and keyboard are
declared in the header file IOSTREAM, which we routinely included in our
previous examples
• The classes used specifically for disk file I/O are declared in the file FSTREAM
• The istream and ostream classes are derived from ios and are dedicated to input
and output, respectively.
• The istream class contains such functions as get(), getline(), read(), and the
overloaded extraction (>>) operators,
• while ostream contains put() and write(), and the overloaded
insertion (<<) operators.
MANIPULATORS
• Manipulators are formatting instructions inserted directly into a stream.
• Some of this we have used before such as the manipulator endl, which sends a newline to the
stream and flushes it, also the setw().
• From the two examples, we can see that manipulators are of two types: one type takes
argument and the other don’t take arguments
• Manipulator Purpose
• ws Turn on whitespace skipping on input
• dec Convert to decimal
• oct Convert to octal
• hex Convert to hexadecimal
• endl Insert newline and flush the output stream
• ends Insert null character to terminate an output string
• flush Flush the output stream
• lock Lock file handle
• unlock unlock file handle
These manipulators are inserted directly into the stream. For example, to output var in
hexadecimal format, you can say
cout<< hex << var;
Note: that manipulators affect only the data that follows them in the stream, not the data that
precedes them
The manipulators that take arguments need the IOMANIP header file for these
functions. They include:
Manipulator Argument Purpose
setw() field width (int) Set field width for output
setfill() fill character (int) Set fill character for output
(default is a space)
setprecision() precision (int) Set precision (number of
digits displayed)
setiosflags() formatting flags (long) Set specified flags
resetiosflags() formatting flags (long) Clear specified flags
Functions
• The ios class contains a number of functions that you can use to set the
formatting flags and perform other tasks. Such as ch = fill(); fill(ch); w = width();
width(w); etc.
The istream Class
• The istream class, which is derived from ios, performs input-specific activities, or
extraction. It contains several functions that can be used for extraction or istream
activities, such as get(ch); (it extracts one character into ch.) , get(str); (extract
characters into array str, until ‘\n’.) , get(str, MAX), etc.
The ostream Class
• The ostream class handles output or insertion activities such as << (Formatted
insertion for all basic (and overloaded) types).
put(ch) (Insert character ch into stream.), write(str, SIZE) (Insert SIZE characters
from array str into file.)
Character Input and String Input
• The extraction operator >> can be used to get a single character from the
standard input.
• The member function get( ) can also be used to obtain a single character; this
can be done in two ways:
• get() can be used with no parameters
• get(x) can also be with parameters
CLASSES FOR INPUT AND OUTPUT
C++ provides the following classes to perform output and input of characters
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.
Already we have learnt about and used objects whose types were these classes:
cin is an object of class istream and cout is an object of class ostream
Reading from or Writing to a File
Reading from or writing to a file in C++ requires 3 basic steps:
• Open the file.
• Do all the reading or writing.
• Close the file.
Opening a File
In order to open a file, use the member function open(). Use it as:
• inFile.open(filename, mode);
• outFile.open(filename, mode);
filename is a string that holds the name of the file on disk (including a path
like /cs/practicals/fumbi.txt if necessary).
mode is a string representing how you want to open the file. Most often you'll
open a file for reading (ios::in) or writing (ios::out or ios::app).
Examples:
ifstream inFile;
ofstream outFile;
inFile.open(inputFilename, ios::in);
outFile.open(outputFilename, ios::out);
Reading from or Writing to a File
Once a file has been successfully opened, you can read from it in the same way as
you would read with cin or write to it in the same way as you write using cout.
infile >> filename;
Outfile << output; e.g outfile<< “this is C++ ioFile”
Closing a File
• When done with a file, it must be closed using the member function close().
• inFile.close();
• outFile.close();
Example writing to a file
1. #include<iostream>
2. #include<fstream>
3. using namespace std;
4. int main()
5. {
6. ofstream outfile;
7. outfile.open("example.txt");
8. outfile<< "testing files";
9. outfile.close();
10. return 0;
11. }
Example Reading from and writing to a file
#include <iostream>
#include <fstream> int a, b, c;
using namespace std;
infile >> a >> b;
int main()
{ c = a*b;
ifstream infile;
outfile<<"The multiplcation of two nos
ofstream outfile;
is" << c;
infile.open("inRecord");
outfile.open("outfile.txt"); infile.close();
outfile.close();
}