You are on page 1of 24

Programming Fundamentals

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.

put(ch) insert character ch into stream

write(str, size) insert size character from array str into file

seekp(position) set distance in bytes of file pointer from start of file

seekp(position, seek_dir) set distance in bytes of file pointer from the


specified in file . seek_dir can be ios::beg, ios::end,
ios::cur

Pos= tellg() returns position of file pointer.


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 declared in the <fstream> header
file and
 are derived directly or indirectly from the
classes istream and ostream class.
Writing data using ofstream class: Is derived from ostream calss
and is used for write to a 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.

The steps to reading or writing a file are:


To perform file processing in C++, header files <iostream> and
<fstream> must be included in your C++ source file.
1) to open the file 
2) to read or write the data from/to the file 
3) to close the file
Following constructor takes a file name as a string to create an output
stream object to write the file −
ofstream aFile("C:/My/myfile1.txt");

This code creates a file called myfile.txt and inserts a sentence


into it in the same way we are used to do with cout, but using
the file stream myfile instead.

Once you have Ofstream object in hand, then there is a list of


helper methods, which can be used to write to stream or to do
other operations on the stream.
Formatted I/O

 Informatted I/O numbers are stored on disk as a series of


characters.
 Thus 77, rather than being stored as 4-byte type int, is stored
as the character ‘7’, ’7’ and this can be inefficient for numbers.
 There are several potential formatting problems .
 First you must separate numbers with non numeric characters.
 Second strings must be separated with white spaces.
 Opening a File with open() function
 A file must be opened before you can read from it or write to
it.
Either ofstream or fstream object may be used to open a file
for writing.
 And ifstream object is used to open a file for reading purpose
only.
 Followingis the standard syntax for open() function, which is
a member of
fstream, ifstream, and ofstream objects.
ofstream aFile;
aFile.open("C:/My/myfile.txt");
Closing the file with close() function

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.

For that, we call the stream's member function close. This


member function closes the file:

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.

 istream class which is derived from ios performs input


specific activities or for reading data from files.

 << extraction for all basic types.


 get(ch) extract one character into ch
 get(str,Max) extract up to max characters into
array str.
 get(str, DELIM) extract characters into array str,
until specified until DEMIL
character.
 getline(str,DELIM) extract characters into array str,
 read(str,max) extract up to max characters into
array str, Until EOF
 seekg() set distance in bytes of file pointer from
the start of the file
 seekg(pos, seek_dir) set distance in bytes of file pointer
from the specified place in the file

 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.

These are called the current get position and


current put position.

These values specify the byte number in the file


where the next read or write operation will take
place.
 Specify the position:
 Seekp() is used to specify the position in bytes in the file
where the next write operation will take place.
 It is used in two ways.
 Seekp(-2, ios::end)
 Set the put pointer
to 2 bytes before
the end of the file.
Anatomy of C++ basic program

You might also like