Input/output Streams and Files: Sushant Bhattarai
Input/output Streams and Files: Sushant Bhattarai
Input/output Streams
and Files
Sushant Bhattarai
Stream Based input/output
Er.Sushant Bhattarai
Stream Classes and
their Hierarchy
C++ has a number of stream classes for
performing I/O operations.
Different streams are used to represent
different kinds of data flow.
Stream Classes and
their Hierarchy
Stream Classes and
their Hierarchy
The ios class is base class of all stream classes
and contains most of stream features.
1. ios : It is the base class for other stream classes.
2. istream: It is the child class of ios and has i/p
related functions, data, constants and operators.
3. ostream: It is the child class of ios
Stream Classes and
their Hierarchy
4) fstreambase : It is the base class for fstream
5) fstream : It provides facility for i/o operations,
6) ifstream : It provides i/p operations on file.
7) iostream : It provides both i/o operations.
8) ofstream
8.2 Unformatted
Input/output
Er.Sushant Bhatta
Unformatted I/O
Er.Sushant Bhatta
Formatted I/O
a) width()
ios Stream Class Member
Functions and Flags
a) width()
ios Stream Class Member
Functions and Flags
b) precision()
The default precision of float type
numbers is six.
cout.precision
ios Stream Class Member
Functions and Flags
b) precision()
ios Stream Class Member
Functions and Flags
b) precision()
Standard Manipulators
c)setf()
Description of Format Flags Bit field
Left justified output ios::left ios::adjusted
Right justified output ios::right ios::adjusted
Padding after sign ios::internal ios::adjusted
Scientific notation ios::scientific ios::floatfield
Fixed point notation ios::fixed ios::floatfield
c)setf()
Standard Manipulators
c)setf()
Standard Manipulators
c)unsetf()
It is also a member of ios class which is
used to clear the flag specified
Standard Manipulators
c)unsetf()
Standard Manipulators
c)unsetf()
User Defined Manipulators
Sushant Bhattarai
File I/O
Er.Sushant Bhatta
Classes for Operations
with Files
The main classes are
1. ifstream if we want to read data from file, we
create object or file stream of ifstream class.
2. ofstream if we want to write data to a file, we
create object or file stream of ofstream class.
3. fstream if both operations is needed we create
object of the file stream of fstream
8.6 Operations on Files
Er.Sushant Bhatta
Steps in Processing
File
When we use file in computer system for
reading and writing purpose, we have to
perform some activities.
C++ uses the following steps
1.Opening a File.
2.Operation on a File.
3. Close the file
8.7 ASCII and Binary Files
Er.Sushant Bhatta
ASCII Files
Er.Sushant Bhatta
Opening and Closing a
File
Before we read/write from/to a file we must open it.
It is the process of establishing a link between program
and OS.
In C++, we can open a file in two methods.
1. Using constructor of File-related stream class.
2. Using member function open() of stream class.
• When I/O operations on a file are completed, we shall
close it so that it’s resources become available to the
system again. We have to call the stream class member
function close().
stream_object.close()
Using constructor of File-related stream
class.
Syntax:
stream_class stream_object(filename,mode)
Where filename is the name of file to be opened and
mode is an optional parameter which might include
1)ios::in I/P mode.
2)ios::out O/P mode.
3)ios::app append mode.
4)ios::binary binary mode.
5)ios::ate opens a file and set intial
Using constructor of File-related stream
class.
E.g.
ofstream out(“welcome.txt”);
ifstream in(“hello.txt”);
fstream in_out(“myfile.txt”,ios::in || ios::out);
Using constructor of File-related stream
class.
E.g.
ofstream out
out.open(“apple.txt”)
out.close()
ifstream in
in.open(“apple.txt”)
in.close()
fstream o
o.open(“apple.txt”,ios::out)
o.close()
Using member function open() of
stream class.
Er.Sushant Bhatta
Reading/Writing from/to file using
read() and write()
Er.Sushant Bhatta
Direct/Random Access
in a File
Sometimes we might want random access in a
file.
We can access a particular data item placed in
any location without starting from the beginning.
This is called random access.
For random access, we need concept of
pointers.
There are two types of file pointers: get pointer
and put pointer
File Pointers: get pointer and
put pointer
1) seekg()
E.g.
seekg(10,ios::beg)
seekg(10,ios::cur)
seekg(-5,ios::end)
seekg(0,ios::end)
Seekg(0,ios::beg);
File Pointers: get pointer and
put pointer
2) seekp()
It is a member function of ostream class and it moves put pointer
to a specified position.
stream_object.seekp(offset,ref_position)
3) tellg()
This is the member function of istream class and it gives or returns
the curent position of the get pointer.
int_variable = stream_object.tellg();
4)tellp()
This is a member function of ostream class and it gives or returns the
current position of the put pointer.
int_variable = stream_object.tellp();
File Pointers: get pointer and
put pointer