Explain the different types of file stream classes

Estudies4you
types of file stream classes

 Q19. Explain five file stream classes needed for file manipulators.

OR
Explain the different types of file stream classes.
Answer:
Types of File Stream Classes

A file is a collection of related data stored in particular area on the disk.
The data transfer can take place in two ways,
1. Data transfer between the console unit and the program.
2. Data transfer between the program and a disk file.

File streams are used to transfer data between program and a disk file. Therefore a file stream acts as an interface between the programs and the files. The file stream I/O operations are similar to, consolestream I/O operations, The stream that provides the data to the program is called as an input stream and the stream that is used to receive the data from the program is called as an output stream. An input stream is used to extract the data from a file and an output stream is used to insert the data to a file. Performing input operations on file streams requires creation of an input stream and linking it with the program and the input file. Similarly performing output operations on file streams requires establishment of an output stream with the necessary links with the program and the output file.
The figure below shows the file stream hierarchy.
Figure: File Stream Class Hierarchy
C++ contains several classes that are used to perform file I/O operations. These are ifstream, ofstream and fstream classes which are derived from fstream base class and iostream class. The file stream classes are declared in the header file “fstream.h”.

1. ifstream Class
This class supports input operations on the files. It inherits the functions get( ), getline( ), read( ), seekg( ) and tellg( ) from istream class. When the file is opened in default mode, it contains open( ) function.
The functions of ifstream class are discussed below,
(i) get()
This function is used to read a single character from the file. The get pointer specifies the character which has to be read from the file. This function belongs to fstream class.

(ii) getline()
This function reads a line of text which ends with a newline character. It cam be called using cin object as follows,
cin.getline( line, size)
Where line is a variable that reads a line of text.
Size is number of characters to be read getline() function reads the input until it encounters ‘\n’ or size ⎼1 characters are read. When ‘\n’ is read.  it is not saved instead it is replaced by the null character.

(iii) read()
This function reads a block of data of length specified by an argument ‘n’. This function reads data sequentially. So when the EOF is reached before the whole block is read then the buffer will contain the elements read until EOF.
General syntax,
istream & read(char*str, size n);

(iv) seekg()
This function is used to shift the input pointer (i.e., get) to the given location. It belongs to ifstream. It is a file manipulator.

(v) tellg()
This function is used to determine the current position of the input pointer. It, belongs to ifstream class.

2. ofstream Class
This class supports output operations on the files. It inherits the functions put(), seekp(), tellp() and write() from ostream class. When the file is opened in default mode, it also contains the open() function.
The functions of ofstream class are discussed below,
(i) put();
This function is used to write a single character to the file. A stream object specifies to which file the character should be written. The character will be located at the position specified by the put pointer. This function belongs to fstream class.

(ii) . seekp()
This function is used to shift the output pointer (i.e., put) to the given location. It belongs to ofstream class. It is also a file manipulator.

(iii) tellp()

This function is used to determine the current position of the output pointer. It belongs to ofstream class.

(iv) write()
This function displays a line on the screen. It is called using cout object as follows,
cout.write(line, size)
Where line is the string to be displayed.
Size is the number of characters to be displayed.
If the size of string is greater than the line (i.e., text to be displayed) then write() function stop displaying on encountering null character but displays beyond the bounds of line.

(3) fstream Class
This class provides support for both input and output operations on the files at the same time. It inherits all the member functions of the istream and ostream classes through iostream class. when a file is opened in default mode, it also contains open() function.

(4) fstream Baseclass
This is the base class for ifstream, ofstream and fstream classes. It provides the operations that are common to the file streams. It contains open( ) and class( ) functions.

(5) filebuf Class
This class is used to set the file buffers to read and write operations. it contains the functions open() and close(). It also contains a constant named Openport which is used
in opening of file stream classes using open() function.
(i) open()
This function is used to create new files as well as open existing files.
General Form
Stream-object open (“file name”, mode);
Where, ‘mode’ specifies the purpose of opening a file. This ‘mode’ argument is optional, if it is not given, then the prototype of member functions of the classes ifstream and ostream uses the default, values for this argument. The default value are,
ios ∷ in for ifstream functions i.e., open in read only mode
and ios ∷ out for ofstream functions i.e., open.in write only mode.

(ii) close()
A file can be closed using member function close(). This function neither takes any parameter nor returns any value.
Syntax
stream_name.close();
Here,
stream_name is the name of the stream to which file is linked.


To Top