Professional Documents
Culture Documents
in
C++
(PRACTICAL#14)
Objective: To become familiar with Files and Streams.
Stream Definition:
A stream refers to a flow of data.
A stream is a sequence of bytes or sequence of characters .
Or In C++ a stream is composed of bytes. It's called a stream
because it is like a stream of water that continues to flow.
Different Streams are use to represent different kinds of data
flow.
In C++ a stream is represented by and object of particular
class
For example
cout and cin are stream objects .
cout is a stream object . It is standard output stream object.
cin is the standard input standard input stream object.
Stream Class Hierarchy
C++ ios stream class is the parent class of all stream classes .
It contains member functions and constants common to all input and
output operations.
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 calss.
C++ ios stream class is the parent class of all stream classes
It contains member functions and constants common to all
input and output operations.
The most common features are:
Formatting flags
Manipulators and Functions
File operations mode
Formatting flags are the enum definitions in the ios. They
act and on/off switches the specify the choices for input and
output.
How to use Formatting flags:
All flags can be set or reset using setf() and unsetf() functions
Example:
cout.setf(ios::left);
turns on left justification for all output directed to cout.
Manipulators: are the formatting instructions inserted
directly into the stream.
You can also manipulate flags indirectly, using the
manipulators
https://doc.bccnsoft.com/docs/cppreference_en/io_flags.ht
ml
The ostream class : Is used to write data to files:
Function Purpose
<< Formatted insertion for all basic types.
write(str, size) insert size character from array str into file
ofstream is used to create a file and write data into it. The stream would
create a file, if it doesn't already exist, before opening it for output.
Once this member function is called, the stream object can be re-
used to open another file, and the file is available again to be
opened by other processes.
aFile.close()
Reading data to Disk Files.
Character I/O
Put and get functions writes and read outputs and inputs a
single character.
File operations Mode: or the mode bits
Specify how a stream will be opened
We need to tell the computer the purpose of opening our file. For e.g.- to write on the file, to read
from the file, etc. These are the different modes in which we can open a file.
File Pointers: Each file object has
associated with two integer values called
the get pointer and put pointer.