Computer Programming Lec 13

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Computer Programming

Maria
Faculty of CS Dept.
Contents:
File Handling
Advantages of Files
Types of Files
File Access Methods
File Handling
A file is a collection of related records.
A records contain data about an entity.
A file can be used to save any type of data.
Files are stored on secondary storage.
Data is stored in file permanently.
File Handling
Normally, a program inputs data from user and stored
in variables as temporally.
When the program ends all data stored in variables is
also destroyed.
The user has to input data again from the keyboard
when next time the program is executed.
The data has to be entered each time when program is
executed and that's waste time.
A user may also type wrong data at different time.
File Handling
A data file can be used to provide input to program.
It can also be used to store the output of program
permanently.
If a program gets input from a file instead of keyboard,
it will get the same data each time it is executed.
There will be far less chances of errors.
Advantages of Files
Files can store large amount of data permanently.
Files can be updated easily.
One file can be used by many programmers for same
input.
Files same time and efforts to input data from
keyboard.
Types of Files
C++ provides two types of files, categorization is based
on how data is stored in files.
1) Text files
2) Binary files
Types of Files
Text Files Binary Files
 A type of file that stored data  A type of file that stores data as
as printable and readable non readable binary code is called
character. binary file.
 An object file of C++ program is
 A source program of C++ is
an example of binary file.
an example of text file.  It consist of binary code that is
 A user may easily read and converted by the compiler from
view the content of text file. source file.
 It can also be printed to get a  The user cannot read the
hard copy. contents of this file.
 It can only be read and processed
by computer.
File Access Methods
The way in which the file can be accessed is called file
access method. It depends on the manner in which
data is stored in the files.
1) Sequential Access Method
2) Random Access Method
Sequential Access Method
It is used to access data in the same sequence in which
it is stored in a file.
This methods read and write data in a sequence.
In order to read the last record, it has to read all
records stored before the last one.
Random Access Method
It is used to access any data item directly without
accessing the preceding data.
This method does not read or write data in sequence.
It is a very fast access method as compared to
sequential access method.
A searching operation with random access method
takes far less time to find data item.

You might also like