Ch-9:File Handling

Meaning : File handling is the process of  managing data file(s). It organizes the files by storing and retrieving data to and  from the data file.

 

File :  File is the collection of related data and information in a computer. There are two types of file.

(i)                 Program file : It is the collection of commands and keywords sequentially, in order to solve a particular problem. Program file is developed in programming language. It has an extension .BAS, .PRG,. CPP etc.

(ii)                Data file : It the collection of data under different fields of a particular topic stored in computer  It is linked with program file.  There are two types of data file. They are

(a)    Sequential access data file : The data file where data are organized in form of records sequentially, one after another. It access the records from top to bottom orderly unless end of file.

 

Advantages:

*. It is simple and natural way of managing data.

 

Disadvantages:

*. It is very slow and very difficult to update the data 

 

(b)   Random Access data file : The data file where data are accessed randomly without any sequence of record is called random access data file. The address of each record is derived from the key field called record pointer.

 

Advantages:

*. Direct access of record is possible, thus faster.

 *. Files can be easily maintained up to date.

 

Disadvantages:

*.There is danger of data corruption and deletion.

 

Modes of opening data file :

 

The way or purpose of the file to be opened is called file mode. Data file are opened to carry out the following activities.

*.To write /store data

*.To retrieve/read data

*.To append/add data

There are three modes of opening data file. They are

 

(i)                 Output mode : Data file is opened in this mode, in order to write or store data to the sequential data file

(ii)                Append mode : Data file is opened in this mode,  to add/append data at the end of existing data file.

(iii)             Input mode : Data file is opened in this mode, to read or retrieve data from sequential data file.

 

Keywords related to data file:

 

(i) OPEN:  This statement is used to open the data file for reading or writing or adding data to the data file. It creates buffer to the memory for I/O operation.

Syntax: OPEN<file$> FOR MODE AS #<FILE NUM>

 

(ii) CLOSE : It terminates or closes all open data files

      and devices.

Syntax : CLOSE #<FILE NUM>,#<FILENUM>,…

 

(iii) WRITE # : This statement is used to write/store data to the sequential data file.

Syntax: WRITE#<FILE NUM>,data,……

 

(iv) PRINT #:  : It is also used to write/store data to the sequential data file.

Syntax: PRINT#<FILE NUM>,data,……


 

1.      Write a program to store records regarding the information of students it includes information like roll number, name, class, gender, age and address in sequential data file “student.txt”.

OPEN “STUDENT.TXT” FOR OUTPUT AS #1

CLS

DO

INPUT “ENTER ROLL, NAME, ADDRESS AND CLASS OF A STUDENT”; R, N$, C,G$, A,AD$

WRITE #1, R, N$, C,G$, A,AD$

INPUT “DO YOU WANT TO CONTINUE”; X$

LOOP WHILE UCASE$(X$) = “Y”

CLOSE #1

END

 

2.      A sequential data file “student.txt " contains few records under the field’s roll number, name, class, gender, age and address. Write a program to add few more records in the same sequential data file. 

OPEN “STUDENT.TXT” FOR APPEND AS #1

CLS

DO

INPUT “ENTER ROLL, NAME, CLASS, GENDER AGE AND ADDRESS OF A STUDENT”; R, N$, C,G$, A,AD$

WRITE #1, R, N$, C,G$, A,AD$

INPUT “DO YOU WANT TO CONTINUE”; X$

LOOP WHILE UCASE$(X$) = “Y”

CLOSE #1

END

 

3.      A sequential data file "student.txt" contains roll number, name, class, gender, age and address fields of information about display all the information of students.

OPEN “STUDENT.TXT” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT  #1, R, N$, C,G$, A,AD$

PRINT  R, N$, C,G$, A,AD$A, AD

WEND

CLOSE #1

END

 

4.      A sequential data file "student.txt" contains roll number, name, class, gender, age and address fields. WAP to count and display information of students who studies in class 10 only.

OPEN “STUDENT.TXT” FOR INPUT AS #1

CLS

WHILE NOT EOF(1)

INPUT #1, R, N$, C,G$, A,AD$

IF C = 10 THEN

PRINT R, N$, C,G$, A,AD$

X= X+1

END IF

WEND

PRINT "TOTAL STUDENTS WHO READS IN CLASS 10 ARE"; X

CLOSE #1

END

 

5.      A sequential data file "student.txt" contains roll number, name, class, gender, age and address fields of information about display information of students whose address is Kathmandu only.

OPEN “STUDENT.TXT” FOR INPUT AS #1

CLS

WHILE NOT EOF(1)

INPUT #1, R, N$, C,G$, A,AD$

IF UCASE$(A$) = “KATHMANDU” THEN

PRINT R, N$, C,G$, A,AD$

END IF

WEND

CLOSE #1

END

 

6.      A sequential data file “student.txt " has several records having fields student's roll number, name, class, gender, age and address. Write a program that reads all the records and displays only those records whose name starts with 'K'.

OPEN “STUDENT.TXT” FOR INPUT AS #1

CLS

WHILE NOT EOF(1)

INPUT #1, R, N$, C,G$, A,AD$

Z$ = LEFT$(N$,1)

IF UCASE$(Z$) = "K" THEN

PRINT R, N$, C,G$, A,AD$

END IF

WEND

CLOSE #1

END

 

7.      A sequential data file called "student.txt" has stored data under the field heading roll number, name, class, gender, and address. Write a program to display all the information of those students whose gender are "F" and age is more than 14.

OPEN “STUDENT.TXT” FOR INPUT AS #1

CLS

WHILE NOT EOF(1)

INPUT #1, R, N$, C,G$, A,AD$

IF UCASE$(G$) = “F” AND A>14 THEN

PRINT R, N$, C,G$, A,AD$

END IF

WEND

CLOSE #1

END

 

8.      Write a program to delete all those records whose address is "Kathmandu" from  data file named "record.txt" which contains roll number, name, address and class of several students.

OPEN “RECORD.TXT” FOR INPUT AS #1

OPEN "TEMP.TXT" FOR OUTPUT AS #2

CLS

WHILE NOT EOF(1)

INPUT #1, R, N$, A$, C

A$ = UCASE$(A$)

IF A$ <> "KATHMANDU" THEN

            WRITE #2, R, N$, A$, C

END IF

WEND

KILL "RECORD.TXT" NAME "TEMP.TXT" AS "RECORD.TXT"

CLOSE #1

CLOSE #2

END

 

9.      A sequential data file “class.dat” has several records having field roll number, name, class, gender, age and address. WAP to copy all the records of class 10 into newly record file “new.dat.”

OPEN "class.dat" FOR INPUT AS #1

OPEN "new.dat" FOR OUTPUT AS #2

WHILE NOT EOF(1)

INPUT #1, roll, name$, class, gender$, age, address$

IF class = 10 THEN

WRITE #2, roll, name$, class, gender$, age, address$

END IF

WEND

CLOSE #1

CLOSE #2

END

 

10.  Write a program to calculate and display percentage of all the students along with the roll number and name.

OPEN "marks.dat" FOR INPUT AS #1

WHILE NOT EOF(1)

 INPUT #1, roll, name$, m1, m2, m3, m4, m5

total = m1 + m2 + m3 + m4 + m5

percentage = total / 5

PRINT roll, name$, percentage

WEND

CLOSE #1

END

 

11.  Write a qbasic program to read all the records from the data file "record.txt" containing name and salary. Then find and display the lowest, highest and average salary of the company.

 

OPEN "RECORD.TXT" FOR INPUT AS #1

INPUT #1, N$, SAL

S = SAL

H = SAL

CLOSE #1

OPEN "RECORD.TXT" FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, N$, SAL

PRINT N$, SAL

IF S > SAL THEN

S = SAL

END IF

IF H < SAL THEN

H = SAL

END IF

SUM = SUM + SAL

N = N + 1

WEND

AVE = SUM / N

PRINT AVE

PRINT S

PRINT H

CLOSE #1

END

12. A sequential data file called "staff.txt" has stored data under the field headings: Name, address , post and date of birth (MM-DD-YYYY). Write a program to display all the information of those employees who were born between the years 1970 AD to 2020 AD.

 Open "staff.txt" for input as #1

While not eof (1)

Input #1, n$,a$, p$, dob$

X$ = Right$(DOB$, 4)

d = val(X$)

If d>=1970 and d<=2020 then

Print n$,a$, p$, dob$

End if

Wend

close #1

End

 


Popular posts from this blog

Computer

Sequential Programs