Thursday, April 9, 2020

Chapter - 11 File handling in QBASIC


Program file
A program file has a set of instructions and codes which are needed for data processing. It has .BAS as an extension.
Data file
A data file has collection of related data stored in a secondary storage device of computer. Such a collection of data in a row is known as a record. A record in a data file contains the detailed information of a person or anything. For example, a record of a data file related to ‘employee’ consists of employee name, post, department, date of birth and salary. A field is a particular data of a record.



Types of data file
1. Sequential Access File
Sequential access file, data is stored in sequential order. The data of a sequential access file can only be accessed sequentially. For example, if a sequential access data file has stored name of a person, address and age of some people then data of this file need to access in the same order as the data are stored in a row. Since the accessing of data from the sequential data file is done in sequential order, the accessing data takes long time if the data file contains large records.

2. Random access data file
A random access file allows us to write or read data from any location of the file. We can read or write any record directly in a random file without searching through all the records that precede it. Thus, reading and writing of data is faster than sequential access data file.

How to open a sequential data file?

OPEN statement is used to open a sequential data file.
Syntax 1: OPEN FOR AS # Filenumber
Where,
Filename is the name of the sequential data file.
Mode determines the operation of data file like OUTPUT, INPUT and APPEND. The different modes and their purposes are listed below.

OUTPUT mode
To create a new sequential data file and store data in it.



INPUT mode
To retrieve the contents of the existing data file.

APPEND mode
To add more records in the existing data file.
File number is a number from 1 to 255 that identifies the data file.

REM to create a new sequential data file and to store data.
CLS
OPEN “employee.dat” FOR OUTPUT AS # 1
DO
        INPUT “Enter employee name “;n$
        INPUT “Enter address”; a$
        INPUT “Enter post”;p$
        INPUT “Enter salary”; sal
        WRITE#1, n$,a$,p$,sal
        INPUT “Do you need more records (y/n)”; ch$
LOOP WHILE UCASE$(ch$) = “Y”
CLOSE#1
END



REM to display the content of datafile
CLS
OPEN “employee.dat” FOR INPUT AS # 1
PRINT “Name”, “Address”, “Post”, “Salary”
WHILE NOT EOF (1)
        INPUT#1, n$,a$,p$,sal
        PRINT n$,a$,p$,sal
WEND
CLOSE#1
END



REM add more records to the data file
CLS
OPEN “employee.dat” FOR APPEND AS # 1
DO
        INPUT “Enter employee name “;n$
        INPUT “Enter address”; a$
        INPUT “Enter post”;p$
        INPUT “Enter salary”; sal
        WRITE#1, n$,a$,p$,sal
        INPUT “Do you need more records (y/n)”; ch$
LOOP WHILE UCASE$(ch$) = “Y”
CLOSE#1
END

No comments:

Post a Comment