Chapter-13 File Handling
1. Fill in the banks:
(a) A program is a collection of instructions written in a computer language.
(b) The files of computer are classified into program file and data file.
(c) QBASIC supports two types of data file. They are Random Access data file and Sequential Access data file.
(d) Different modes of sequential data file are INPUT, OUTPUT and APPEND.
(e) WRITE statement is used to write data to the opened data file.
(f) INPUT statement is used to read data from the opened data file.
(g) Append mode allows to add records to the existing data file.
2. State whether the following statements are true or false:
(a) A data file is a collection of programs. FALSE
(b) A program can be used to create a data file. TRUE
(c) Only one program can use a data file. FALSE
(d) A data file can be created,modified or deleted. TRUE
(e) In a sequential access data file, data can be read from any position. FALSE
(f) Data can be added or appended in a sequential data file. TRUE
(g) Data can be copied from one data file to another. TRUE
(h) Output mode is used to create new data file. TRUE
3. Write the syntax and purpose of the following statements and functions:
(a) OPEN
Syntax: OPEN “Filename.Extension” FOR Data File | Program File |
---|---|
1. A collection of the related data stored in the hard disk or any other secondary storage devices separately from the program that uses it. | 1. A file which contains instructions, which are used to manipulate data and give the output and stored in the secondary storage devices is called program file. |
Mode of File | Function |
---|---|
Output Mode | For writing data or information to a new file |
Input Mode | For reading data or information from a data file |
Append Mode | Appending information or data to the end of an existing file |
Input Mode | Output Mode |
1. Open a file in reading mode. | 1. Open a new file to write data. |
2. It never creates a new data file. | 2. It always creates a new data file. |
3. Old records will not be deleted in this mode. | 3. Old records will be deleted in this mode. |
Output Mode | Append Mode |
---|---|
1. It always creates a new data file. | 1. If the specified file does not exist , APPEND creates it. |
2. Old records will be deleted in this mode. | 2. Old records will not be deleted in this mode. |
To create a data file that stores name, address and salary for at least ten different persons.
ANSWER:
OPEN "EMPLOYEE.TXT" FOR OUTPUT AS #1
FOR P = 1 TO 10
INPUT "ENTER NAME,ADDRESS AND SALARY", NAME$, ADDRESS$, SALARY
WRITE #1, NAME$, ADDRESS$, SALARY
NEXT P
CLOSE #1
END
Write a program to store name, roll no and marks for three subjects in a sequential data file named “STUDENT.DAT”. The program should terminate according to the user’s choice.
ANSWER:
OPEN "STUDENT.DAT" FOR OUTPUT AS #1
DO
INPUT “Enter name,roll and marks in 3 subjects ” , NAME$, ROLL, SUB1, SUB2, SUB3
WRITE #1, NAME$, ROLL, SUB1, SUB2, SUB3
INPUT “DO YOU WANT TO ADD SOME MORE RECORDS (Y/N) ” , CH$
LOOP WHILE UCASE$(CH$) = “Y”
CLOSE #1
END
Write a program to input data from the data file named “student.dat” which, contains name, roll no and marks for three subjects for n number of students. Calculate total and percentage and display the result on the screen.
ANSWER:
CLS
OPEN "STUDENT.DAT" FOR INPUT AS #1
PRINT "NAME", "ROLL", "SUB1", "SUB2", "SUB3", "TOTAL", "PERCENTAGE”
WHILE NOT EOF(1)
INPUT #1, NAME$, ROLL, SUB1, SUB2, SUB3
TOTAL = SUB1 + SUB2 + SUB3
PERCENTAGE = TOTAL / 3
PRINT NAME$, ROLL, SUB1, SUB2, SUB3, TOTAL, PERCENTAGE
WEND
CLOSE #1
END
Write a program to open a data file named “STUDENT.DAT” and add some data on it. Fields of the data file are “Name”, “Address”, “Telephone”.
ANSWER:
OPEN “STUDENT.DAT” FOR APPEND AS #1
DO
INPUT “Enter name,address and telephone ”; NAME$, ADDRESS$, TELEPHONE$
WRITE #1, NAME$, ADDRESS$, TELEPHONE$
INPUT “DO YOU WANT TO ADD SOME MORE RECORDS”; CH$
LOOP WHILE UCASE$(CH$) = “Y”
CLOSE #1
END
Write a program to ask item name, sold quantity, amount and salesman’s name and store them in a sequential data file “SALES.DAT”. Ask the user if he/she wants to enter another data or not.
ANSWER:
OPEN “SALES.DAT” FOR OUTPUT AS #1
DO
INPUT “Enter item name, sold quantity , amount , salesman’s name ” ; ITEMNAME$, SOLDQTY , AMOUNT ,SALES NAME$
WRITE #1, ITEMNAME$ , SOLDQTY , AMOUNT , SALES NAME$
INPUT “ DO YOU WANT TO ADD SOME MORE RECORDS ” ; CH$
LOOP WHILE UCASE$(CH$) = “Y”
CLOSE #1
END
Write a program to read all the data of the data file named “SALES.DAT”, where fields are unknown. Display the result on the screen.
ANSWER:
OPEN “SALES.DAT” FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, ITEMNAME$ , SOLDQTY , AMOUNT , SALES NAME$
PRINT ITEMNAME$ , SOLDQTY , AMOUNT , SALES NAME$
WEND
CLOSE #1
END
A data file named “STUDENT.DAT” contains a number of records. Write a program to display all records from records number 5 to 10.
ANSWER:
OPEN "STUDENT.TXT" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, SN, NAME$
IF SN >= 5 AND SN <= 10 THEN
PRINT SN, NAME$
END IF
WEND
CLOSE #1
END
Write a program to read all the data from the data file “SALES.DAT” and copy them to another data file named “TEMP.DAT”.
ANSWER:
OPEN "SALES.DAT" FOR INPUT AS #1
OPEN "TEMP.DAT" FOR OUTPUT AS #2
WHILE NOT EOF(1)
INPUT #1, ITEMNAME$, SOLDQTY, AMOUNT, SALESMANNAME$
WRITE #2, ITEMNAME$, SOLDQTY, AMOUNT, SALESMANNAME$
WEND
CLOSE #1, #2
END
A data file contains names, marks of three subjects and total marks. Write a program to read the name from the keyboard and display information on the monitor. If the required data is located inside the data file, otherwise display, a message “DATA NOT FOUND”.
ANSWER:
OPEN “MARKS.DAT” FOR INPUT AS #1
INPUT “ ENTER THE NAME FOR SEARCH ”, NS$
FLAG = 0
DO
INPUT #1, NAME$ , SUB1 , SUB2 , SUB3
IF NAME$ = SN$ THEN
PRINT NAME$ , SUB1 , SUB2 , SUB3
FLAG=1
EXIT DO
END IF
LOOP WHILE NOT EOF(1)
IF FLAG=0 THEN PRINT “DATA NOT FOUND”
CLOSE #1
END
A data file contains the names of students , marks of three subjects and total marks of n number of students. Write a program to display the data for only those students whose total marks is greater than 100 but less than 150.
ANSWER:
OPEN “MARKS.DAT” FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, NAME$ , SUB1 , SUB2 , SUB3 , TOTAL
IF TOTAL > 100 AND TOTAL < 150 THEN
PRINT NAME$ , SUB1 , SUB2 , SUB3 , TOTAL
END IF
WEND
CLOSE #1
END
Write a program to input the name of a student to delete his/her data from the data file named “STUDENT.DAT”. The data file contains name, roll no and marks for three different subjects.
ANSWER:
OPEN “MARKS.DAT” FOR INPUT AS #1
OPEN “TEMP.DAT” FOR OUTPUT AS#2
INPUT “ ENTER THE NAME TO BE DELETED ”, D$
DO
INPUT #1,N$,R,S1,S2,S3
IF N$<>D$THEN
WRITE #2,N$,R,S1,S2,S3
ELSE
PRINT “DELETED DATA”;N$,R,S1,S2,S3
END IF
LOOP WHILE NOT EOF(1)
CLOSE #1,#2
KILL “MARKS.DAT”
NAME “TEMP.DAT” AS “MARKS.DAT”
END
A data file named “STUDENT.DAT” contains some records. Write a program to content total records of the data file.
ANSWER:
OPEN “ STUDENT.DAT” FOR INPUT AS #1
C = 0
DO
LINE INPUT #1, N$
C = C + 1
LOOP WHILE NOT EOF(1)
CLOSE #1
PRINT “TOTAL RECORDS” ; C
END
A data file named “STUDENT.DAT” contains some records having fields name and marks in three subjects. Write a program to display all the records on the monitor if the marks in each subject are > = 60.
ANSWER:
OPEN “STUDENT.DAT” FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, NAME$ , SUB1 , SUB2 , SUB3
IF SUB1 >= 60 AND SUB2 >= 60 AND SUB3 >= 60 THEN
PRINT NAME$ , SUB1 , SUB2 , SUB3
END IF
WEND
CLOSE #1
END
A data file named “OFFICE.DAT” contains some records having fields name, post and salary. Write a program to count the total number of officers in the office.
ANSWER:
OPEN “STUDENT.DAT” FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, NAME$ , POST$, SALARY
IF POST$ = “OFFICER” THEN
C = C + 1
PRINT NAME$ , POST$, SALARY
END IF
WEND
CLOSE #1
END
Debug the following programs and test them in computer.
REM to add or append
OPEN “C:\SAL.DAT” FOR OUTPUT AS #1
DO
CLS
INPUT “ENTER NAME” ; N$
INPUT “ENTER POST” ; P$
INPUT “ENTER SALARY” ; S
WRITE #2, N$ , P$, S
INPUT “ WANTS TO SUPPLY MORE Y/N” ; CH
LOOP WHILE UCASE$(CH)= “Y”
CLOSE #2,#1
END
Corrected Program
REM to add or append
OPEN “C:\SAL.DAT” FOR APPEND AS #1
DO
CLS
INPUT “ENTER NAME” ; N$
INPUT “ENTER POST” ; P$
INPUT “ENTER SALARY” ; S
WRITE #1, N$ , P$, S
INPUT “ WANTS TO SUPPLY MORE Y/N” ; CH$
LOOP WHILE UCASE$(CH$)= “Y”
CLOSE #1
END
REM to copy data from “SAL.DAT” to “TEMP.DAT”
OPEN “SAL.DAT” FOR INPUT AS #1
OPEN “TEMP.DAT” FOR OUTPUT AS #2
CLS
DO
INPUT #2,N$,P$,S
WRITE # 2, N$ , P$, S
LOOP WHILE NOT EOF( )
CLOSE #2,#1
END
Corrected Program
REM to copy data from “SAL.DAT” to “TEMP.DAT”
OPEN “SAL.DAT” FOR INPUT AS #1
OPEN “TEMP.DAT” FOR OUTPUT AS #2
CLS
DO
INPUT #1 , N$ , P$ , S
WRITE # 2 , N$ , P$ , S
LOOP WHILE NOT EOF( 1 )
CLOSE #1 , #2
END
No comments:
Post a Comment