Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Class notes Bca001

Rating
-
Sold
-
Pages
59
Uploaded on
27-05-2025
Written in
2024/2025

Bca ooops subject unit 4

Institution
Course

Content preview

Unit 4

Files - Introduction

A file is collection of data or information that has a name, called the filename. Files are
stored in secondary storage devices such as floppy disks and hard disks.

The main memories of a computer such as random access memory or read-only memory
are not used for the storage of files. This is because the main memory of a computer is limited and
cannot hold a large amount of data. Another reason is that the main memory is volatile; that is,
when the computer is switched off, the contents of RAM vanish.




Fig. Communication between program, file, and output device

As shown in Figure, the data read from the keyboard are stored in variables. Variables are
created in RAM (type of primary memory).. It is also possible to read data from secondary storage
devices. When data are read from such devices, they are placed in the RAM and then, console I/O
operations are used to transfer them to the screen. RAM is used to hold data temporarily.




1

, Data communication can be performed between programs and output devices or between
files and programs. File streams are used to carry the communication among the above-mentioned
devices. The stream is nothing but a flow of data in bytes in sequence. If data were received from
input devices in sequence, then it is called a source stream, and if the data were passed to output
devices, then it is called a destination stream. Figure shows the input and output streams. The
input stream brings data to the program, and the output stream collects data from the program. In
another way, the input stream extracts data from the file and transfers it to the program; whereas
the output stream stores the data in the file provided by the program.




Fig. Input and output streams

File Stream Classes

A stream is nothing but a flow of data. In the object-oriented programming, the streams are
controlled using the classes.




2

, The ios class is the base class. All other classes are derived from the ios class. These
classes contain several member functions that perform input and output operations. The
streambuf class has low-level routines and provides interface to physical devices.

The istream and ostream classes control input and output functions, respectively. The ios
is the base class of these two classes. The functions get(), getline(), and read() and overloaded
extraction operators (>>) are defined in the istream class. The functions put(), write(), and
overloaded insertion operators (<<) are defined in the ostream class. The iostream class is also a
derived class. It is derived from istream and ostream classes. The classes ifstream, ofstream and
fstream are derived from istream ,ostream and iostream respectively. These classes handle input
and output with the disk files. The header file fstream.h contains a declaration of ifstream,
ofstream, and fstream classes, including isotream.h file. This file should be included in the
program while doing disk I/O operations.

Details of File Stream Classes:

Class Description
Sets the file buffers to read and write. It holds constant openprot used in
filebuf function open() and close() as a member.
The fstreambase acts as a base class for fstream, ifstream, and ofstream.
fstreambase The functions such as open() and close() are defined in fstreambase
Provides input operations on files. Contains open() with default input
ifstream mode. Inherits the functions as get(), getline(), seekg(), tellg(), and
read() from istream class
Provides output operations on files. Contains open() with default output
ofstream mode. Inherits the functions as put(), seekp(), write(), and tellp()from
ostream class
Provides support for simultaneous input/output file stream class. Inherits
fstream
all functions from istream and ostream classes through iostream.




3

, Steps of File Operations

Before performing file operations, it is necessary to create a file. The operation of a file
involves the following basic activities:

Specifying suitable file name
Opening the file in desired mode
Reading or writing the file (file processing)
Detecting errors
Closing the file

File Opening: In order to perform operations, we have to create a file stream object and
connecting it with the file name. The classes ifstream, ofstream, and fstream can be used for
creating a file stream. The selection of the class is according to the operation that is to be carried
out with the file. The operation may be read or write. Two methods are used for the opening of a
file. They are as follows:
Constructor of the class

Member function open()

1. Constructor of the class:

When objects are created, a constructor is automatically executed, and objects are
initialized. In the same way, the file stream object is created using a suitable class, and it is
initialized with the file name. The constructor itself uses the file name as the fist argument and
opens the file. The class ofstream creates output stream objects, and the class ifstream creates
input stream objects.

Consider the following examples:

a) ofstream out (“text”);
b) ifstream in(“list”);

In the statement (a), out is an object of the class ofstream; file name text is opened, and
data can be written to this file. The file name text is connected with the object out. Similarly,
in the statement (b), in is an object of the class ifstream. The file list is opened for input and
4

Written for

Institution
Course

Document information

Uploaded on
May 27, 2025
Number of pages
59
Written in
2024/2025
Type
Class notes
Professor(s)
Pooja
Contains
All classes

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
silentgirl

Get to know the seller

Seller avatar
silentgirl Self
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
11 months
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions