file handling in qbasic for Grade 10
1. [SEE 2074]
Q: Write a program to store Roll no., Name, Class and Address of any five students.
OPEN "student.dat" FOR OUTPUT AS #1
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
2. [SLC 2068]
Q: A sequential data file called “student.dat” contains records under the fields name, english, nepali and computer. Write a program to add some more records in the same sequential data file.
OPEN "student.dat" FOR APPEND AS #1
DO
INPUT "Enter Name:"; N$
INPUT "Enter English:"; E
INPUT "Enter Nepali:"; Nep
INPUT "Enter Computer:"; C
WRITE #1, N$, E, Nep, C
INPUT "Do you want to continue (Y/N)?"; ans$
LOOP WHILE UCASE$(ans$) = "Y"
CLOSE #1
3.
Q: A sequential data file “RECORD.DAT” stores Name, Address and Salary of employees. WAP to add some more records in the data file “RECORD.DAT”. The program should terminate with user choice.
OPEN "RECORD.DAT" FOR APPEND AS #1
DO
INPUT "Enter Name:"; N$
INPUT "Enter Address:"; A$
INPUT "Enter Salary:"; S
WRITE #1, N$, A$, S
INPUT "Do you want to continue (Y/N)?"; ans$
LOOP WHILE UCASE$(ans$) = "Y"
CLOSE #1
4. [SEE 2073]
Q: Create a data file to store the records of few employees having Name, Address, Post, Gender and Salary fields.
OPEN "employee.dat" FOR OUTPUT AS #1
FOR i = 1 TO 5
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
NEXT i
CLOSE #1
5.
Q: Create a sequential data file ’Price.dat’ to store item name, quantity and Rate also calculate total amount (total = Quantity × Rate). Program should terminate according to the user’s choice.
OPEN "Price.dat" FOR APPEND AS #1
DO
INPUT "Enter Item Name:"; Item$
INPUT "Enter Quantity:"; Q
INPUT "Enter Rate:"; R
T = Q * R
WRITE #1, Item$, Q, R, T
INPUT "Do you want to continue (Y/N)?"; ans$
LOOP WHILE UCASE$(ans$) = "Y"
CLOSE #1
6.
Q: 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
FOR i = 1 TO 15
INPUT "Enter Name:"; N$
INPUT "Enter Marks in English:"; E
INPUT "Enter Marks in Nepali:"; Nep
INPUT "Enter Marks in Computer:"; C
Total = E + Nep + C
Percent = Total / 3
WRITE #1, N$, E, Nep, C, Total, Percent
NEXT i
CLOSE #1
7.
Q: Store SIDNO, name, address and Telephone number of five students and display the records on monitor in sequential data file “STDINFO”.
OPEN "STDINFO" FOR OUTPUT AS #1
FOR i = 1 TO 5
INPUT "Enter SIDNO:"; SID
INPUT "Enter Name:"; N$
INPUT "Enter Address:"; A$
INPUT "Enter Telephone:"; T$
WRITE #1, SID, N$, A$, T$
NEXT i
CLOSE #1
OPEN "STDINFO" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, SID, N$, A$, T$
PRINT SID, N$, A$, T$
WEND
CLOSE #1
8.
Q: A sequential data file “Address.inf” contains serial no, name, address, telephone and email_id. WAP to record as many records as the user wants. The serial number should be generated automatically like 5001, 5003, 5005.
OPEN "Address.inf" FOR APPEND AS #1
Sno = 5001
DO
INPUT "Enter Name:"; N$
INPUT "Enter Address:"; A$
INPUT "Enter Telephone:"; T$
INPUT "Enter Email:"; E$
WRITE #1, Sno, N$, A$, T$, E$
Sno = Sno + 2
INPUT "Do you want to continue (Y/N)?"; ans$
LOOP WHILE UCASE$(ans$) = "Y"
CLOSE #1
9.
Q: A Sequential data file called "SEE.DAT" has stored data under Symbol No., Name, English, Nepali, and Computer. Write a program to display all the information of those students whose marks in Computer is more than 80.
OPEN "SEE.DAT" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, Sym, N$, Eng, Nep, Comp
IF Comp > 80 THEN
PRINT Sym, N$, Eng, Nep, Comp
END IF
WEND
CLOSE #1
10.
Q: 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
WHILE NOT EOF(1)
INPUT #1, N$, M1, M2, M3
IF M1 < 40 OR M2 < 40 OR M3 < 40 THEN
PRINT N$, M1, M2, M3
END IF
WEND
CLOSE #1
11.
Q: 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 present in that data file.
OPEN "Result.DAT" FOR INPUT AS #1
count = 0
WHILE NOT EOF(1)
INPUT #1, N$, M1, M2, M3
Total = M1 + M2 + M3
Percent = Total / 3
count = count + 1
IF Percent > 60 AND Percent < 80 THEN
PRINT N$, M1, M2, M3, Percent
END IF
WEND
PRINT "Total Records = "; count
CLOSE #1
12.
Q: Write a program to read all the records from the data file “STUDENT.TXT” and display all the records where the fields name are unknown.
OPEN "STUDENT.TXT" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, N$, Class$, Roll
IF UCASE$(N$) = "UNKNOWN" THEN
PRINT N$, Class$, Roll
END IF
WEND
CLOSE #1
13.
Q: 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
WHILE NOT EOF(1)
INPUT #1, School$, Principal$, Address$, Contact$
IF UCASE$(Address$) = "KATHMANDU" OR UCASE$(Address$) = "PALPA" THEN
PRINT School$, Principal$, Address$, Contact$
END IF
WEND
CLOSE #1
14.
Q: A data file “INFO.DAT” has numerous records with name, address, age, and telephone numbers. Write a program to read all the records and print those with address “NEPAL” and age >15.
OPEN "INFO.DAT" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, N$, Address$, Age, Tel$
IF UCASE$(Address$) = "NEPAL" AND Age > 15 THEN
PRINT N$, Address$, Age, Tel$
END IF
WEND
CLOSE #1
15.
Q: A sequential data file called 'ADDRESS.DAT' contains NAME, AGE, CITY and TELEPHONE fields. Write a program to display all the contents of that data file.
OPEN "ADDRESS.DAT" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, N$, Age, City$, Tel$
PRINT N$, Age, City$, Tel$
WEND
CLOSE #1
16. [SEE 2073]
Q: 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
count = 0
WHILE NOT EOF(1)
INPUT #1, Book$, Author$, Price
count = count + 1
WEND
PRINT "Total records = "; count
CLOSE #1
17.
Q: 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.
OPEN "EMP.DAT" FOR INPUT AS #1
count = 0
WHILE NOT EOF(1)
INPUT #1, N$, Address$, Phone$
PRINT N$, Address$, Phone$
count = count + 1
WEND
PRINT "Total Records = "; count
CLOSE #1
18.
Q: 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.
OPEN "nabil.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, Name$, Amount, Time, Rate
SI = (Amount * Time * Rate) / 100
PRINT Name$, Amount, Time, Rate, SI
WEND
CLOSE #1
19.
Q: A sequential data file “SALARY.DAT” contains Employee-Code, Employee Name, Post, Basic-Salary. Write a program to display those records whose Basic-salary is between 10000 to 15000 and Post is ‘OFFICER’.
OPEN "SALARY.DAT" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, Code$, Name$, Post$, Salary
IF Salary >= 10000 AND Salary <= 15000 AND UCASE$(Post$) = "OFFICER" THEN
PRINT Code$, Name$, Post$, Salary
END IF
WEND
CLOSE #1
20.
Q: A data file “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
count = 0
WHILE NOT EOF(1)
INPUT #1, N$, Post$, Salary
IF UCASE$(Post$) = "MANAGER" THEN
count = count + 1
END IF
WEND
PRINT "Total Managers = "; count
CLOSE #1
21.
Q: A sequential data file “emp.dat” contains name, post and salary fields of employees. Write a program to display all the information of employees along with tax amount (tax = 15% of salary).
OPEN "emp.dat" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, N$, Post$, Sal
Tax = Sal * 0.15
PRINT N$, Post$, Sal, Tax
WEND
CLOSE #1
22.
Q: A data file “Salary.Dat” contains the information of employee regarding their name, post and salary. Write a program to display all the information of employee whose salary is greater than 15000 and less than 40000.
OPEN "Salary.Dat" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, N$, Post$, Sal
IF Sal > 15000 AND Sal < 40000 THEN
PRINT N$, Post$, Sal
END IF
WEND
CLOSE #1
23.
Q: Write a program that reads the ”INFO.DAT” file that has several records such as name, address, gender, post, and salary. The program displays those records whose sex is male and salary more than 10,000 and also counts the total number of records in that file.
OPEN "INFO.DAT" FOR INPUT AS #1
count = 0
WHILE NOT EOF(1)
INPUT #1, N$, Address$, Gender$, Post$, Sal
count = count + 1
IF UCASE$(Gender$) = "MALE" AND Sal > 10000 THEN
PRINT N$, Address$, Gender$, Post$, Sal
END IF
WEND
PRINT "Total Records = "; count
CLOSE #1
24.
Q: 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
WHILE NOT EOF(1)
INPUT #1, N$, Address$, Sal
IF LEFT$(UCASE$(Address$),1) = "S" OR LEFT$(UCASE$(Address$),1) = "D" THEN
PRINT N$, Address$, Sal
END IF
WEND
CLOSE #1
25.
Q: Write a program to open a data file “record.dat” that contains name, address, date of birth, email and telephone number of some employees. Now display all those records whose date of birth is in current month.
OPEN "record.dat" FOR INPUT AS #1
CURMONTH = VAL(MID$(DATE$,4,2)) 'extract current month
WHILE NOT EOF(1)
INPUT #1, N$, Address$, DOB$, Email$, Tel$
DOBMONTH = VAL(MID$(DOB$,4,2))
IF DOBMONTH = CURMONTH THEN
PRINT N$, Address$, DOB$, Email$, Tel$
END IF
WEND
CLOSE #1
26.
Q: A sequential data file “Record.dat” has few records related to name, address, post and DOB(mm/dd/yyyy). WAP to display the records of all those who were born between 1971 to 1999.
OPEN "Record.dat" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, N$, Address$, Post$, DOB$
Year = VAL(RIGHT$(DOB$,4))
IF Year >= 1971 AND Year <= 1999 THEN
PRINT N$, Address$, Post$, DOB$
END IF
WEND
CLOSE #1
27.
Q: Write a Qbasic program that reads the "EMP.DAT" file with Name, Address, Post and Salary columns from E: drive that has 500 records of employees and displays only its last 50 records.
OPEN "E:\EMP.DAT" FOR INPUT AS #1
count = 0
WHILE NOT EOF(1)
INPUT #1, N$, Address$, Post$, Sal
count = count + 1
rec$(count) = N$ + " " + Address$ + " " + Post$ + " " + STR$(Sal)
WEND
FOR i = count - 49 TO count
PRINT rec$(i)
NEXT i
CLOSE #1
28.
Q: 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 "student.dat" FOR INPUT AS #1
count = 0
WHILE NOT EOF(1)
INPUT #1, N$, Class$, Roll
count = count + 1
IF count >= 50 AND count <= 60 THEN
PRINT N$, Class$, Roll
END IF
WEND
CLOSE #1
29.
Q: 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
count = 0
WHILE NOT EOF(1) AND count < 10
INPUT #1, N$, Phone$, Email$
PRINT N$, Phone$, Email$
count = count + 1
WEND
CLOSE #1
30.
Q: A data file named “EMP.DAT” contains some records with the fields Code, Name, Post and Salary. Write a program to print odd position records of the data file.
OPEN "EMP.DAT" FOR INPUT AS #1
count = 0
WHILE NOT EOF(1)
INPUT #1, Code$, N$, Post$, Sal
count = count + 1
IF count MOD 2 <> 0 THEN
PRINT Code$, N$, Post$, Sal
END IF
WEND
CLOSE #1
31.
Q: A sequential data file named “abc.dat” has several records having fields name, roll and class. Write a program to copy all the records of class 10 into a newly created file new.dat.
OPEN "abc.dat" FOR INPUT AS #1
OPEN "new.dat" FOR OUTPUT AS #2
WHILE NOT EOF(1)
INPUT #1, N$, Roll, Class$
IF Class$ = "10" THEN
WRITE #2, N$, Roll, Class$
END IF
WEND
CLOSE #1
CLOSE #2
32.
Q: A data file named “record.dat” contains name, age and salary for n number of persons. Write a program to input a name to search data from a data file. If the data is not found, then display the message “Data not found in the list”.
OPEN "record.dat" FOR INPUT AS #1
found = 0
INPUT "Enter name to search:"; search$
WHILE NOT EOF(1)
INPUT #1, N$, Age, Sal
IF UCASE$(N$) = UCASE$(search$) THEN
PRINT N$, Age, Sal
found = 1
END IF
WEND
IF found = 0 THEN
PRINT "Data not found in the list"
END IF
CLOSE #1
33.
Q: A sequential data file 'Student.dat' contains registration number, student name, address and date of birth of some students. Write a program that asks a user to input a registration number and displays the record of the particular registration if present.
OPEN "Student.dat" FOR INPUT AS #1
found = 0
INPUT "Enter Registration Number:"; reg
WHILE NOT EOF(1)
INPUT #1, R, N$, A$, DOB$
IF R = reg THEN
PRINT R, N$, A$, DOB$
found = 1
END IF
WEND
IF found = 0 THEN
PRINT "Record not found"
END IF
CLOSE #1
34.
Q: WAP that asks a post of the employee and displays his/her records from the sequential data file “XYZ.REC” having fields Name, Post, Dept and Salary.
OPEN "XYZ.REC" FOR INPUT AS #1
INPUT "Enter Post to search:"; P$
WHILE NOT EOF(1)
INPUT #1, N$, Post$, Dept$, Sal
IF UCASE$(Post$) = UCASE$(P$) THEN
PRINT N$, Post$, Dept$, Sal
END IF
WEND
CLOSE #1
35.
Q: Delete some records from “neps.dat” file where computer asks 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 #2
INPUT "Enter name to delete:"; del$
WHILE NOT EOF(1)
INPUT #1, N$, A$, T$
IF UCASE$(N$) <> UCASE$(del$) THEN
WRITE #2, N$, A$, T$
END IF
WEND
CLOSE #1
CLOSE #2
KILL "neps.dat"
NAME "temp.dat" AS "neps.dat"
36.
Q: A sequential data file “marks.dat” contains information such as student’s name, marks obtained in math, science and computer. Write a program that increases 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
WHILE NOT EOF(1)
INPUT #1, N$, Math, Sci, Comp
IF Comp < 40 THEN Comp = Comp + 10
WRITE #2, N$, Math, Sci, Comp
WEND
CLOSE #1
CLOSE #2
KILL "marks.dat"
NAME "temp.dat" AS "marks.dat"
37.
Q: A sequential data file “RECORD.DAT” contains different records under fields: rollno, name, address and percentage. Write a program to edit a record and display both edited and unedited records on the screen to compare them side by side.
OPEN "RECORD.DAT" FOR INPUT AS #1
OPEN "temp.dat" FOR OUTPUT AS #2
INPUT "Enter roll number to edit:"; editroll
WHILE NOT EOF(1)
INPUT #1, Roll, N$, A$, Per
IF Roll = editroll THEN
PRINT "Old Record:"; Roll, N$, A$, Per
INPUT "Enter New Name:"; N1$
INPUT "Enter New Address:"; A1$
INPUT "Enter New Percentage:"; Per1
WRITE #2, Roll, N1$, A1$, Per1
PRINT "Edited Record:"; Roll, N1$, A1$, Per1
ELSE
WRITE #2, Roll, N$, A$, Per
END IF
WEND
CLOSE #1
CLOSE #2
KILL "RECORD.DAT"
NAME "temp.dat" AS "RECORD.DAT"
Comments
Post a Comment