SEE File Handling - 4 marks

1. Write a program to store Roll No, Name, Class and Address of any five students.
OPEN "ABC.TXT" FOR OUTPUT AS #1 FOR I = 1 TO 5 INPUT "Enter roll, name,class and address : ",R,N$,C,A$ WRITE #1,R,N$,C,A$ NEXT I CLOSE #1 END
2. A data file "hospital.dat" contains information about some staffs. The records are staffname,post,shift,age. Write a program to insert 15 records to data file.
OPEN "hospital.dat" FOR APPEND AS #1 FOR I = 1 TO 15 Input "Enter staff name, post, shift and age : ",N$,P$,S$,AGE WRITE #1,N$,P$,S$,AGE NEXT I CLOSE #1 END
3. Write a program that asks the item's name, rate and quantity and stores into "sales.txt". The user can supply 10 records in each execution of the program.
OPEN "sales.txt" FOR OUTPUT AS #1 FOR I = 1 TO 10 INPUT "Enter item's name, rate and quantity ",name$,rate,quantity WRITE #1,name$,rate,quantity NEXT I CLOSE #1 END
4. Write a program to create a sequential data file named "Employee.dat" to store Name, Post, Address, and Salary for the number of employees. The program should terminate on the user's choice.
OPEN "employee.dat" FOR OUTPUT AS #1 DO INPUT "Enter employee name, post , address and salary ",name$,post$,adr$,sal WRITE #1,name$,post$,adr$,sal INPUT " Want to add more records"; choice$ LOOP WHILE UCASE$(choice$)="Y" CLOSE #1 END
5. Write a program to create a sequential data file "salary.dat" to store programmer's Name, salary and post according to the need of the user.
Open "salary.dat" for output as #1 Do Input "Enter employee name, post and salary ",name$,post$,salary WRITE #1,name$,post$,salary input " Want to add more records"; choice$ Loop while(ucase$(choice$))="Y" Close #1 End
6. Write a program to ask students' name, class and marks secured in three subjects. Store the data in a sequential data file "RESULT.TXT" along with the total marks. Make a provision to ask users to enter other records.
Open "result.txt" for output as #1 top: Input "Enter name, class and marks of three subjects ",n$,c,s1,s2,s3 total = S1 + S2 + S3 WRITE #1,n$,c,s1,s2,s3,total Input "Want to add another records ",choice$ If ucase$(choice$)="Y" then goto top Close #1 End
7. Write a program to input the Name, Address and Post of some employees and store them in a file named "emp.txt" along with a serial number for each record.
Open "emp.txt" for output as #1 top: sn = 1 Input "Enter name, address and post ",n$,a$,p$ write #1,sn,n$,a$,p$ sn = sn + 1 Input "Want to add another records ",choice$ If ucase$(choice$)="Y" then goto top Close #1 End
8.Write a program that asks students' name, roll and class and stores into "class.txt" only those records who are studying in class 10. Users can supply the records as per his/her need.
Open "class.txt" for output as #1 top: Input "Enter name,roll and class ",n$,roll,class If class=10 then write #1,n$,roll, class End If Input "Want to add another records ",choice$ If ucase$(choice$)="Y" then goto top Close #1 End
9.Write a program that asks student's names and marks in English, Math and Computer. Store only those records that are passed in all the subjects into the data file named "pass.dat". The user will be asked whether to input another record or not. [Note: pass mark for all the subjects is 40.]
Open "pass.txt" for output as #1 top: Input "Enter name, marks in Eng,Math and Computer ",n$,e,m,c If e>=40 and m>=40 and c>=40 then write #1,n$,e,m,c End If Input "Want to add another records ",choice$ If ucase$(choice$)="Y" then goto top Close #1 End
10. A sequential data file called "record.txt" contains NAME,AGE,CITY and TELEPHONE fields. Write a program to display all the contents of that data file.
Open "record.txt" for input as #1 Print "Name","Age","City","Telephone" while not eof(1) Input #1,n$,age,city$,tel$ Print n$,age,city$,tel$ Wend End
11. A sequential data file "EMP.TXT" contains name, post and salary fields of information about employees. Write a program to display all the information of the employee along with the tax amount also(tax is 15% of salary).
Open "emp.txt" for input as #1 Print "Name","Post","Salary","Tax" While not eof(1) Input #1,name$,post$,salary Tax = (SALARY * 15)/100 Print name$,post$,salary,tax Wend End
12. A data file "STAFF.dat" has stored records of few employees with EmpID, FirstName, LastName, Post and Salary. Write a program to display all the records of the employees whose salary is more than 40,000.
Open "staff.dat" for input as #1 Do while not eof(1) Input #1, empid, f$, l$, p$, salary If salary>40000 then print empid, f$, l$, p$, salary End if Loop Close #1 End
13. A sequential data file called "SEE.TXT" has stored data under the field heading Symbol No, Name, English, Nepali, Maths and Computer. Write a program to display all the information of those students whose marks in Computer is more than 80.
Open "SEE.TXT" for input as #1 Do while not eof(1) Input #1, symbolnum, NAME$, eng, nep, maths,comp If comp>80 then print symbolnum; NAME$; eng; nep; maths; comp End if Loop Close #1 End
14. A sequential data file called "Record.txt" has stored data under the file heading Roll No, Name, Gender, English, Nepali, Maths and Computer. Write a program to display all the information of those students whose gender are "F" and obtained marks in the computer is more than 90.
Open "Record.txt" for input as #1 print "Roll"; "Name"; "Gender"; "English"; "Maths"; "Computer" While not eof(1) Input #1, Roll, Name$, Gen$, Eng, Maths, Comp If UCASE$(Gen$)="F" and Comp>90 then Print Roll; Name$; Gen$; Eng; Maths; Comp End if Wend Close #1 End
15. A sequential data file "class.txt" has several records with the field's students' name, roll and class. Write a program that reads all the records and displays only those records whose roll number is less than 10.
Open "class.txt" for input as #1 Print "Roll", "Name", "Class" While not eof(1) Input #1, Roll, Name$, class If Roll < 10 then Print Roll, Name$, class End if Wend Close #1 End
16. A sequential data file "pass.dat" has several records having fields student's name and marks in English, Math and Computer. Write a program that reads all the records and displays only those records whose name starts with 'A' and also counts the total number of records stored in the file.
Open "pass.dat" for input as #1 While not eof(1) Input #1, Name$, eng,math,comp C = C + 1 p$ = ucase$(left$(NAME$, 1)) If p$="A" then Print Name$,eng,math,comp End if Wend Print "total number of record is" ; C Close #1 End
17. A doctor keeps name, age and sex of his patients in a sequential data file "PATIENT.DAT". Write a QBASIC program to display only male patient records also count the total number of patients in a file.
OPEN "PATIENT.DAT" FOR INPUT AS #1 PRINT "Name","Age","Sex" WHILE NOT EOF(1) INPUT #1, NAME$,AGE,SEX$ C = C + 1 IF UCASE$(SEX$)="MALE" THEN PRINT NAME$,AGE,SEX END IF WEND PRINT "Total number of patients ",C END
18. 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 WHILE NOT EOF(1) INPUT #1,NAME$,SUB1,SUB2,SUB3 C = C + 1 PER = (SUB1 + SUB2 + SUB3) / 3 IF PER>60 AND PER<80 THEN PRINT NAME$,PER END IF WEND PRINT "TOTAL NUMBER OF RECORD IS " ; C Close #1 End
19. A sequential data file "staff.txt" has several records related to name, post , date of birth (mm-dd-yyyy) and salary. Write a program to display the records of those people who were born between the years 2000 to 2020 as well as count and display the total number of records.
Open "staff.txt" for input as #1 While not eof (1) Input #1, n$, p$, dob$, sal c = c + 1 a$ = Right$(DOB$, 4) d = val(a$) If d>=2000 and d<=2020 then Print n$,p$, dob$, sal End if Wend close #1 Print "total records= "; c End
20. A sequential data file "class.txt" has several records having fields student's name, class and roll number. Write a program to copy all the records from "class.txt" into another new file "stduent.txt".
Open "class.txt" for Input as #1 Open "student.txt" for Output as #2 While not eof(1) Input #1, name$, class, roll Write #2, name$, class, roll Wend Close #1,#2 Print "Data Copied Successfully!" End
21. A sequential data file "records.txt" has several records having fields Employee's Name, Post and Salary. Write a program to increase the salary of all employees by 10%.
Open "records.txt" for input as #1 Open "temp.txt" for output as #2 while not eof(1) Input #1, name$,post$,salary SALARY = SALARY + (SALARY * 10) / 100 Write #2, name$,post$,salary Wend Close kill "records.txt" name "temp.txt" as "records.txt" Print "Salary has been updated by 10%" End
22. Write a program to read the data from "INFO.TXT" that contains student name, class, roll no , DOB(MM-DD-YYYY) and address. Write/copy all the data whose DOB is current month to the data file "INFO.TXT".
Open "info.txt" for input as #1 Open "temp.txt" for output as #2 While not eof (1) Input #1, n$, c, r, dob$, A$ If left$(dob$,2)=left$(date$,2) then Write #2, n$,c,r,dob$,a$ End if Wend close Kill "info.txt" Name "temp.txt" as "info.txt" Print "Data copied successfully!" End
23. A sequential data file “RECORDS.TXT” contains name, address, phone number and DOB of few people. Write a program to count the total number of records stored in a data file and display the records of only those people whose name is five character long.
Open "RECORDS.TXT" for input as #1 c=0 While not eof(1) Input #1, name$ , address$ , phone$ , DOB$ c = c + 1 a = len(NAME$) If a=5 then print name$ , address$ , phone$ , DOB$ End if Wend Close #1 Print "Total records=";c End
24. Write a program to read product name, quantity, grade and price of 20 products from “product.dat”. Then count only those records where price is greater than 200 and grade="A".
Open "product.dat" for input as #1 c=0 while not eof(1) Input #1,name$,quantity,grade$,price If price>200 and grade$="A" then c = c + 1 End if Wend Close #1 Print "Total number of products having price greater than 200 and grade A =";c End
25. "score.dat" is a sequential data file which contains marks scored by students in a test . WAP to count and display all the students who has passed in all the subjects along with the calculation of their total marks. Assume field structure of data stored in the file is regno(registration number), English, Nepali, Science, Compulsory mathematics)
open "score.dat" FOR INPUT AS #1 WHILE NOT EOF(1) INPUT #1,REGNO$,ENG,NEP,SCI,MATHS IF ENG>=40 AND NEP>=40 AND SCI>=40 AND MATHS>=40 THEN PRINT REGNO$,ENG,NEP,SCI,MATHS C = C + 1 END IF WEND CLOSE #1 PRINT "No. of Passed Students : " ; C END
Bonus programs on file handling
(1) To create data file "STUDENT.DAT" to store Name, Roll No, Class and Section of students.
OPEN "STUDENT.DAT" FOR OUTPUT AS #1 DO INPUT "Enter student name, roll, class and section ", NAME$,ROLL,CLASS,SEC$ WRITE #1,NAME$,ROLL,CLASS,SEC$ INPUT " Want to add more records ", CHOICE$ LOOP WHILE UCASE$(CHOICE$)="Y" CLOSE #1 END
(2) To store Name, Address , Age and Salary of employees in data file "STAFF.DAT"
OPEN "STAFF.DAT" FOR OUTPUT AS #1 DO INPUT "Enter name, address , age and salary: ", NAME$,ADR$,AGE,SAL WRITE #1,NAME$,ADR$,AGE,SAL INPUT " Want to add more records ", CHOICE$ LOOP WHILE UCASE$(CHOICE$)="Y" CLOSE #1 END
(3) To add records in data file created in (1)
OPEN "STUDENT.DAT" FOR APPEND AS #1 DO INPUT "Enter student name, roll, class and section ", NAME$,ROLL,CLASS,SEC$ WRITE #1,NAME$,ROLL,CLASS,SEC$ INPUT " Want to add more records ", CHOICE$ LOOP WHILE UCASE$(CHOICE$)="Y" CLOSE #1 END
(4) To display all records of data file “STUDENT.DAT” created in (1)
OPEN "STUDENT.DAT" FOR INPUT AS #1 PRINT "Name","Roll No","Class","Section" WHILE NOT EOF(1) INPUT #1, NAME$,ROLL,CLASS,SEC$ PRINT NAME$,ROLL,CLASS,SEC$ WEND CLOSE #1 END
(5) To search record of studetent on the basis of roll
OPEN "STUDENT.DAT" FOR INPUT AS #1 INPUT "ENTER ROLL : ";R FLAG = 0 WHILE NOT EOF(1) INPUT #1,NAME$,ROLL,CLASS,SEC$ IF ROLL=R THEN PRINT NAME$,ROLL,CLASS,SEC$ GOTO AA ELSE FLAG = 1 END IF WEND IF FLAG=1 THEN PRINT "RECORD NOT FOUND" END IF AA: CLOSE #1 END
(6) To display those records whose salary is greater than 15000 from the data file “STAFF.DAT” created in (2)
OPEN "STAFF.DAT" FOR INPUT AS #1 PRINT "Name","Address","Age","Salary" WHILE NOT EOF(1) INPUT #1,NAME$,ADR$,AGE,SAL IF SAL>15000 THEN PRINT NAME$,ADR$,AGE,SAL END IF WEND CLOSE #1 END
(7) To delete the records whose age is greater than 60 from the data file "STAFF.DAT"
OPEN "STAFF.DAT" FOR INPUT AS #1 OPEN "TEMP.DAT" FOR OUTPUT AS #2 WHILE NOT EOF(1) INPUT #1,NAME$,ADR$,AGE,SAL IF AGE<60 THEN WRITE #2,NAME$,ADR$,AGE,SAL END IF WEND CLOSE #1,#2 KILL "STAFF.DAT" NAME "TEMP.DAT" AS "STAFF.DAT" PRINT "Employees having age greather than 60 deleted." END
(8) Write a menu based program to append, display and delete records from the data file.
DECLARE SUB MENU() DECLARE SUB ADDREC() DECLARE SUB DISREC() DECLARE SUB DELREC() CALL MENU INPUT CH SELECT CASE CH CASE 1 CALL ADDREC CASE 2 CALL DISREC CASE 3 CALL DELREC CASE ELSE PRINT "Bye Bye" END SELECT END SUB ADDREC OPEN "STUDENT.DAT" FOR APPEND AS #1 DO INPUT "Enter student name, roll, class and section ", NAME$,ROLL,CLASS,SEC$ WRITE #1,NAME$,ROLL,CLASS,SEC$ INPUT " Want to add more records ", CHOICE$ LOOP WHILE UCASE$(CHOICE$)="Y" CLOSE #1 END SUB SUB DISREC OPEN "STUDENT.DAT" FOR INPUT AS #1 PRINT "Name","Roll No","Class","Section" WHILE NOT EOF(1) INPUT #1, NAME$,ROLL,CLASS,SEC$ PRINT NAME$,ROLL,CLASS,SEC$ WEND CLOSE #1 END SUB SUB DELREC OPEN "STUDENT.DAT" FOR INPUT AS #1 OPEN "TEMP.DAT" FOR OUTPUT AS #2 INPUT "Enter name to delete ",DN$ FLAG = 0 WHILE NOT EOF(1) INPUT #1, NAME$,ROLL,CLASS,SEC$ IF UCASE$(DN$)<>UCASE$(NAME$) THEN WRITE #2,NAME$,ROLL,CLASS,SEC$ ELSE FLAG = 1 PRINT "Data deleted" END IF WEND IF FLAG=0 THEN PRINT "Record not found" END IF CLOSE #1,#2 KILL "STUDENT.DAT" NAME "TEMP.DAT" AS "STUDENT.DAT" END SUB SUB MENU PRINT "1. To add records " PRINT "2. To display record" PRINT "3. To delete record" PRINT "Enter your choice" END SUB
1. Write a program to create a sequential data file “salary.dat” to store programmer’s name, salary and post according to the need of the user.
OPEN "salary.dat" FOR OUTPUT AS #1 DO INPUT "Enter name,salary and post ",name$,salary,post$ WRITE #1,name$,salary,post$ INPUT "More records(Y-Yes/N-No) ";ch$ loop while ucase$(ch$)="Y" CLOSE #1 END
2. Write a program to create a sequential data file named "employ.dat" to store Name, Post, Address and Salary for the number of employees. The program should terminate on user's choice.
OPEN "employ.dat" FOR OUTPUT AS #1 DO INPUT "Enter name ,post , address and salary ";name$,post$,address$,salary WRITE #1,name$,salary,post$ INPUT "more records (Y-Yes/n-No) ";ch$ LOOP WHILE UCASE$(ch$)="Y" CLOSE #1 END
3. 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 in QBASIC to display all the information of those students whose gender are “F” and obtained marks in computer is more than 90.
OPEN "RECORD.DAT" FOR INPUT AS #1 WHILE NOT EOF(1) INPUT #1,ROLL,NAME$,GEN$,ENG,NEP,MATHS,COMPUTER IF GEN$="F" AND COMPUTER>90 THEN PRINT ROLL,NAME$,GEN$,ENG,NEP,MATHS,COMPUTER END IF WEND CLOSE #1 END
4. A sequential data file called "Record.txt" has stored data under the field heading Roll No., Name, Gender, English, Nepali and Math’s and Computer. Write a program in QBASIC to display all the information of female students who pass in all subjects. [Note: Pass nark in all subjects is 40].
END
5. A sequential data file "emp.dat" contains employee's name, address, gender and salary. Write a program in QBASIC to display all the information of employees whose salary is more than Rs.20,000.
END
6. A sequential data file "class.dat" has several records with fields student's name, roll no, address and class. Write a program in QBASIC that reads all the records and displays only those records whose class is 10.
END
7. A sequential data file "abc.txt" has several records with fields student's name, roll no , address and class. Write a program in QBASIC that reads all the records and displays only those records whose address is "Pokhara".
END
8. A sequential data file "abc.txt" has several records with fields student's name, roll no , address, fee and class. Write a program in QBASIC that reads all the records and displays only those records whose fee is greater than 5000.
END
9. A sequential data file "student.txt" consists of Book's name, Author's name and Price of book. Write a program in QBASIC to count and display the total number of records present in the file.
END
10. 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 QBASIC program to display all the female student who passed in all subjects. [Note: Assume pass mark in each subject is 40.]
END