QBASIC File Handling Programming Solutions
TYPE 1: [Store,Write]
1. Create a data file to store the records of few
employees having Name, Address, Post, Gender and Salary fields.
OPEN “std.rec” FOR OUTPUT AS #1
CLS
DO
INPUT “Enter Name”; N$
INPUT “Enter Address”; A$
INPUT “Enter Post”; P$
INPUT “Enter gender”; G$
INPUT “Enter Salary”; S
WRITE #1, N$, A$, P$, G$, S
INPUT “Do you want to continue”; CH$
LOOP WHILE UCASE$(CH$) = “Y”
CLOSE #1
END
2. Write a program to store Roll no., Name, Class and
Address of any five students.
OPEN "Student.dat" FOR OUTPUT AS #1
CLS
FOR I = 1 TO 5
INPUT "Enter Roll No."; R
INPUT "Enter Name"; N$
INPUT "Enter Class"; C
INPUT "Enter Address"; A$
WRITE #1, R, N$, C, A$
NEXT I
CLOSE #1
END
3. Create a sequential data file’Price.dat’ to store item
name, quantity and Rate also calculate total amount (total=Quantity X Rate).
Program should terminate according to the user’s choice.
OPEN “price.dat” FOR OUTPUT AS #1
CLS
DO
INPUT “Enter Item Name”; N$
INPUT “Enter Quantity”; Q
INPUT “Enter Rate”; R
T = Q * R
WRITE #1, N$, Q, R, T
INPUT “Do you want to continue”; CH$
LOOP WHILE UCASE$(CH$) = “Y”
CLOSE #1
END
4. Create a sequential data file’post.dat’ to store name
and marks of any three subjects also calculate total and percentages only for
15 students.
OPEN "post.dat" FOR OUTPUT AS #1
CLS
FOR I = 1 TO 15
INPUT "Enter Name"; N$
INPUT "Enter marks in 1st Subject:”; A
INPUT “Enter marks in 2nd Subject:”; B
INPUT “Enter marks in 3rd Subject:”; C
T = A + B + C
P = T / 3
WRITE #1, N$, A, B, C, T, P
NEXT I
CLOSE #1
END
Note: TYPE 1: [ADD] Just replace
OUTPUT with APPEND
[HW]A sequential data file called “student.dat” contains
some records under the fields name, english, nepali and computer. Write a
program to add some more records in the same sequential data file.
[HW]A sequential data file “RECORD.DAT” store Name,
Address and Salary of employees of an office. WAP to add some more records in
the data file “RECODR.DAT”. The program should terminate with user choice.
TYPE 2:
[Display,Print,Read,View,Retrive]
1. Students' name, class, section and address are stored
in a data file called "STUDENT.DAT" Write a program to print all the
records of students.
OPEN “STUDENT.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, C, S$, A$
PRINT “Name:”; N$
PRINT “Class:”; C
PRINT “Section:”; S$
PRINT “Address:”; A$
WEND
CLOSE #1
END
Write a program to display the first 10 records from a
file named “resource.dat” having fields name, phone and email.
OPEN "RESOURCE.DAT" FOR INPUT AS #1
CLS
FOR I = 1 TO 10
INPUT #1, N$, C, R
PRINT N$, C, R
NEXT I
CLOSE #1
END
2. Employee's name, address, gender and salary are stored
in the "EMP.DAT" sequential data file. Write a QBASIC program that
displays all information about personnel whose salaries exceed 60000.
OPEN "EMP.DAT" FOR INPUT AS #1
CLS
WHILE NOT EOF (1)
INPUT #1, N$, A$, G$, S
IF S > 60000 THEN
PRINT “Name:” N$
PRINT “Address:” A$
PRINT “Gender:” G$
PRINT “Salary:” S
END IF
WEND
CLOSE #1
END
3. A data file, "Salary Dat" contains the
information of the employee regarding their Name, Post and Salary. Write a
program to display all the information of employee whose salary is greater than
15,000 and less than 40,000.
OPEN "Salary.dat" FOR INPUT AS #1
CLS
WHILE NOT EOF (1)
INPUT #1, N$, P$, S
IF S>15000 AND S<40000 THEN
PRINT “Name:”; N$
PRINT “Post:”; P$
PRINT “Salary:”; S
WEND
CLOSE #1
END
4. A data file "PABSON.TXT" contains the
records composed of the fields like school, principal, address, contact. Write
a program in QBasic to display records of the schools located in either
Kathmandu or Palpa
OPEN "PABSON.TXT" FOR INPUT AS #1
CLS
WHILE NOT EOF (1)
INPUT #1, S$, P$, A$, C
IF UCASE$(A$) = “KATHMANDU” OR UCASE$(A$) = “PALPA” THEN
PRINT “School:”; S$
PRINT “Principal:”; P$
PRINT “Address:”; A$
PRINT “Contact:”; C$
WEND
CLOSE #1
END
5. A sequential data file called "Employee.txt"
has stored data under the field headings Employee ID, Name, Gender, Department,
and Salary. Write a program to display all the information of those employees
whose salary is greater than 50,000 and the department is "IT".
OPEN “Employee.txt” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, ID, N$, G$, D$, S
IF S> 50000 AND UCASE$(D$) =”IT” THEN
PRINT “Employee ID:”; ID
PRINT “Name:”; N$
PRINT “Gender:”; G$
PRINT “Department:”; D$
PRINT “Salary:”; S
END IF
WEND
CLOSE #1
END
(H/W) A sequential data file called “Record.txt” has
stored data under the field heading Roll No., Name, Gender, English, Nepali,
Maths and Computer. Write a program to display all the information of those
students whose gender is “F” and obtained marks in computer is more than 90.
6. A sequential data file "EMP.DAT" contains
name, post and salary fields of information about employees. Write a program to
display all the information of employees along with tax amount also (tax is 15%
of salary).
OPEN "EMP.DAT" FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, P$, S
T = 15 / 100 * S
PRINT “Name:” N$
PRINT “Post:”; P$
PRINT “Salary:”; S
PRINT “Tax amount:”; T
WEND
CLOSE #1
END
(H/W) A sequential data file called "data.dat"
has stored data under the field heading item name, quantity and rate. Write a
program to display all the records with total.
(H/W) A sequential data file named “nabil.txt” contains
record of clients of a bank including depositor’s name, deposited amount, time
and rate of interest. Wap to display detail of all depositors including simple
interest.
7. A data file "LIB.TXT" consists of Book's
name, Author's name and price of books. Write a program to count and display
the total number of records present in the file.
OPEN "LIB.TXT" FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, B$, A$, P
C = C + 1
PRINT “Book's name:”; B$
PRINT “Author's name:”; A$
PRINT “Price of books:”; P
WEND
PRINT "Total number of records:"; C
CLOSE #1
END
(H/W) Write a program in QBASIC to open a sequential data
file “EMP.DAT”, which contains employees records: Name, address and phone
number and display all the records as well as total number of records stored in
the file.
8. Write a program which reads records from the file”
Result.DAT” having the fields name and marks of three different subjects and
display only those records whose percentage is greater than 60 and less than
80. Also count the total number of records presenting in that data files.
OPEN "Result.DAT" FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, N$, S, M, E
C=C+1
T=S+M+E
P=T/3
IF P > 60 AND P < 80 THEN
PRINT “Name:”; N$
PRINT “Marks in Science:”; S
PRINT “Marks in Math:”; M
PRINT “Marks in English:”; E
PRINT “Total:”; T
PRINT “Percentage:”; P
WEND
PRINT “Total number of records:”;
CLOSE #1
END
Only For A+
Students and more practice
1. Store SIDNO, name, address and Telephone number of
five students and display the records on monitor in sequential data file
“STDINFO”.
OPEN "STDINFO.DAT" FOR OUTPUT AS #1
CLS
FOR I = 1 TO 5
INPUT "ENTER NAME"; N$
INPUT "ENTER ADDRESS"; A$
INPUT "ENTER TELEPHONE"; T$
WRITE #1, N$, A$, T$
NEXT I
CLOSE #1
OPEN "STDINFO.DAT" FOR INPUT AS #1
CLS
FOR I = 1 TO 5
INPUT #1, N$, A$, T$
PRINT N$, A$, T$
NEXT I
CLOSE #1
END
2. A sequential data file "Student.dat" has
stored data under the field headings: student id, student's name, address and
age of some students. Write a program to copy all the records of those students
whose age is greater than 15 to another file named as "Record.dat".
OPEN "Student.dat" FOR INPUT AS #1
OPEN "Record.dat" FOR OUTPUT AS #2
WHILE NOT EOF(1)
INPUT #1, ID, N$, A$, AG
IF AG > 15 THEN
WRITE #2, ID, N$, A$, AG
END IF
WEND
CLOSE #1
CLOSE #2
END
3. A sequential data file “STD.TXT” contains name and
marks in three different subjects of some students. Write a program to display
only fail student’s records assuming pass marks 40.
OPEN "STD.TXT" FOR INPUT AS #1
CLS
WHILE NOT EOF (1)
INPUT #1, N$, A, B, C
IF A < 40 AND B < 40 AND C < 40 THEN
PRINT “Name:”; N$
PRINT “Marks in 1st subject:”; A$
PRINT “Marks in 2nd subject:”; B$
PRINT “Marks in 3rd subject:”; C$
WEND
CLOSE #1
END
4. A sequential data file has 100 records having field
name, class and roll number. Write a program to display from 50th to
60th records.
OPEN "ABC.DAT" FOR INPUT AS #1
CLS
WHILE NOT EOF (1)
INPUT #1, N$, C, R
D = D + 1
IF D >= 50 AND D <= 60 THEN PRINT N$, C, R
WEND
CLOSE #1
END
5. A data file name “EMP.DAT”, contains number of records
having fields name, post and salary. Write a program to count total number of
“Manager” in the data file.
OPEN "EMP.DAT" FOR INPUT AS #1
CLS
WHILE NOT EOF (1)
INPUT #1, N$, P$, S
IF UCASE$(P$) = “MANAGER” THEN PRINT C = C + 1
WEND
PRINT “Total number of managers:”; C
CLOSE #1
END
6. A sequential data file’post.dat’ has few records
related to name, address, salary. WAP to display the records whose address
begins with ‘S’ or ‘D’
OPEN "POST.DAT" FOR INPUT AS #1
CLS
WHILE NOT EOF (1)
INPUT #1, N$, P$, S
A$ = UCASE$(LEFT$(N$,1))
IF A$ = “S” OR A$ = “D” THEN PRINT N$, P$, S
WEND
CLOSE #1
END
8. A sequential data file called "list.dat" has stored data under the field heading items name quantity and rate. Write a program to update all the records by increasing the rate by 10%.
OPEN "list.dat" FOR INPUT AS #1
OPEN "temp.dat" FOR OUTPUT AS #2
WHILE NOT EOF(1)
INPUT #1, item$, quantity, rate
rate = rate + 10/100 * rate
WRITE #2, item$, quantity, rate
WEND
CLOSE #1, #2
KILL "list.dat"
NAME "temp.dat" AS "list.dat"
END
9. A sequential data file “marks.dat” contains
information such as student’s name, marks obtained in math, science and
computer. Write a program that increase the marks of computer by 10 of those students
who secured less than 40
OPEN "marks.dat" FOR INPUT AS #1
OPEN "temp.dat" FOR OUTPUT AS #2
CLS
WHILE NOT EOF(1)
INPUT #1, N$, A, B, C
IF C > 40 THEN
WRITE #2, N$, A,
B, C
ELSE
C = C + 10
WRITE #2, N$, A,
B, C
END IF
WEND
CLOSE
KILL "marks.dat"
NAME "temp.dat" AS "temp.dat"
END
10. Delete some records from “neps.dat” file where computer ask user to enter the record, which is to be deleted. (Fields are name, address, and telephone number)
OPEN “NEPS.DAT” FOR INPUT AS #1
OPEN “TEMP.DAT” FOR OUTPUT AS #1
CLS
INPUT “Enter name which is to be deleted”; D$
WHILE NOT EOF(1)
INPUT #1, N$, A$, T
IF UCASE$(D$) <> UCASE$(N$) THEN
WRITE #2, N$, A$, T
ELSE
PRINT “Deleted data:”; N$, A$, T
END IF
WEND
CLOSE #1, #2
KILL “NEPS.DAT”
NAME “TEMP.DAT” AS “NEPS.DAT”
END