Programming Fundamentals in C++: Practical#14)
Programming Fundamentals in C++: Practical#14)
in
C++
(PRACTICAL#14)
Objective: To become familiar with Files and Streams.
Stream Definition:
All input and output in C++ involves the use of streams.
A stream is general term which refers to a flow of data.
2) OutPutStream − The OutputStream is used for writing data to a destination. it may be a file,
an array, peripheral device or socket.
InputStream vs OutputStream
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.
The cout object, representing the standard output stream, which is usually
directed to the video display, is predefined object of the
ostream_withassign class. Which is derived from the ostream class.
Similarly cin is an object of the istream_withassign class, which is derived from istream.
The classes used for input and output to the video display and
keyboard are declared in the header file IOSTREAM.
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.
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.