File Handling Questions
Q1. A sequential data file called “student.dat” has stored the data under the file headings stu_id, stu_name, stu_class, and stu_address. Write a program in Q-Basic to display the details of those students who study in grades 5 and 10.
Q2. A sequential data file called “staff.dat” has stored the data under the file headings staff_id, staff_name, and staff_salary. Write a program in Q-Basic to retrieve the records of those students whose salary is greater than 20000 and less than 50000. i.e., 20000< a <50000
Q3. A sequential data file called “student.dat” has stored the data under the file headings stu_id, stu_name, stu_class, and stu_address. Write a program in Q-Basic to display the details of those students whose names start with the letter “p”.
Q4. A sequential data file called “stu.dat” has stored the data under the file headings s_id, s_name, s_class, and s_address. Write a program in Q-BASIC to add more records to the file.
Q5. Write a program in Q-BASIC to store the id, name, and salary of staff until the user says no to the staff.txt file.
Q6. A sequential data file called “item.dat” stores the data under the file headings item_no, item_name, item_price, and item_qty. Write a Q-BASIC program to update each product's price by 10%.
Solution:
Open "student.txt" For Input As #1
Cls
While Not EOF(1)
Input #1, stu_id, stu_name$, stu_class, stu_address$
If stu_class = 5 Or stu_class = 10 Then
Print "Id:"; stu_id
Print "Name:"; stu_name$
Print "class:"; stu_class
Print "Address:"; stu_address$
End If
Wend
Close #1
EndSimple student.dat data file
1,"Ram",5,"Kathmandu"
2,"Sita",10,"Pokhara"
3,"Hari",8,"Lalitpur"
4,"Gita",5,"Bhaktapur"
5,"Ramesh",10,"Chitwan"
6,"Anita",7,"Biratnagar"
Output
Q2. Solution
Open "staff.txt" For Input As #1
Cls
While Not EOF(1)
Input #1, staff_id, staff_name$, staff_salary
If staff_salary > 20000 And staff_salary < 50000 Then
Print "ID:", staff_id
Print "Name:", staff_name$
Print "salary:", staff_salary
End If
Wend
Close #1
EndQ3. A sequential data file called “student.dat” has stored the data under the file headings stu_id, stu_name, stu_class, and stu_address. Write a program in Q-Basic to display the details of those students whose names start with the letter “p”.
Solution
Open "student.txt" For Input As #1
Cls
While Not EOF(1)
Input #1, stu_id, stu_name$, stu_class, stu_address$
If UCase$(Left$(stu_name$, 1)) = "P" Then
Print "Id:"; stu_id
Print "Name:"; stu_name$
Print "class:"; stu_class
Print "Address:"; stu_address$
End If
Wend
Close #1
EndQ4. A sequential data file called “stu.dat” has stored the data under the file headings s_id, s_name, s_class, and s_address. Write a program in Q-BASIC to add more records to the file.
Solution
Open "student.txt" For Append As #1 Cls aa: Input "enter id", id Input "enter name"; stu_name$ Input "Enter class:"; stu_class Input "Enter Address:"; stu_address$ Write #1, id, stu_name$, stu_class, stu_address$ Input "Do you want to add more records?", ch$ If UCase$(ch$) = "Yes" Then GoTo aa: End