0% found this document useful (0 votes)
439 views

File I/O With Streams

File I/O with Streams handles reading and writing files using streams. It uses classes like ifstream for input, ofstream for output, and fstream for both. These classes manage reading from and writing to disk files through functions like open(), close(), read(), write(), and inherit relevant functions from istream, ostream or both. The input stream extracts data from a file during reading and the output stream inserts data during writing.

Uploaded by

Ajay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
439 views

File I/O With Streams

File I/O with Streams handles reading and writing files using streams. It uses classes like ifstream for input, ofstream for output, and fstream for both. These classes manage reading from and writing to disk files through functions like open(), close(), read(), write(), and inherit relevant functions from istream, ostream or both. The input stream extracts data from a file during reading and the output stream inserts data during writing.

Uploaded by

Ajay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

File I/O with Streams:

Many real-life problems handle large volumes of data, therefore we need to use some devices such as
floppy disk or hard disk to store the data.
The data is stored in these devices using the concept of files. A file is a collection of related data
stored in a particular area on the disk.
Programs can be designed to perform the read and write operations on these files.
A program typically involves either or both of the following kinds of data communication:
Data transfer between the console unit and the program.
Data transfer between the program and a disk file.
The input/output system of C++ handles file operations which are very much similar to the console
input and output operations.
It uses file streams as an interface between the programs and the files.
The stream that supplies data to the program is known as input stream and the one that receives data
from the program is known as output stream.
In other words, the input stream extracts or reads data from the file and the output stream inserts or
writes data to the file.

Classes for the file stream operations :


The I/O system of C++ contains a set of classes that define the file handling methods.
These include ifstream, ofstream and fstream.
These classes, designed to manage the disk files, are declared in fstream.h and therefore we must
include this file in any program that uses files.
Details of some useful classes :
filebuf
Its purpose is to set the file buffer to read and write. Contains openprot constant used in the open() of
the filestream classes. Also contains close() and open() as member functions.
fstreambase
Provides operations common to the file streams. Serves as a base for fstream, ifstream and ofstream
classes. Contains open() and close() functions.
ifstream
Provides input operations. Contains open() with default input mode. Inherits the functions get(),
getline(), read(), seekg() and tellg() functions from istream.
ofstream
Provides output operations. Contains open() with default output mode. Inherits put(), seekp(), tellp(),
and write() functions from ostream.
fstream
Provides support for simultaneous input and output operations. Contains open() with default input

mode. Inherits all the functions from istream and ostream classes through iostream.
The ifstream, ofstream and fstream classes are declared in the file fstream.h
The istream and ostream classes are also included in the fstream.h file.

You might also like