File Handling in QBasic

Q. WAP to store employee's id, name, post and department in a data file EMPINFO.DAT
1. OPEN "EMPINFO.DAT" FOR OUTPUT AS #1
   TOP:
   CLS
   INPUT "ENTER ID";I
   INPUT "ENTER NAME"; IN$
   INPUT "ENTER POST"; P$
   INPUT "ENTER DEPARTMENT";D$
   WRITE #1,I,IN$, P$, D$
   INPUT "DO YOU WANT TO CONTINUE? Y/N";CH$
   IF UCASE$(CH$)="Y" THEN GOTO TOP
   CLOSE #1
   END


Q. WAP to write seven customer's name, address, product name and price in sequential data file "product.txt"
2. OPEN "product.txt" FOR OUTPUT AS #1
   FOR I = 1 TO 7
   CLS
   INPUT "ENTER CUSTOMER'S NAME"; N$
   INPUT "ENTER ADDRESS"; A$
   INPUT "ENTER PRODUCT'S NAME"; P$
   INPUT "ENTER PRICE" ; P
   WRITE #1, N$,A$,P$,P
   NEXT I
   CLOSE #1
   END


Q. WAP to store the information of book's number, book's name and writer's name, price and quantity in a sequential data file called "book.rec"
3. OPEN "book.rec" FOR OUTPUT AS #1
   TOP:
   CLS
   INPUT "ENTER BOOK'S ID";ID
   INPUT "ENTER BOOK'S NAME";BN$
   INPUT "ENTER AUTHOR'S NAME";AN$
   INPUT "ENTER PRICE";P
   INPUT "ENTER QUANTITY"; Q
   WRITE #1, ID,BN$,AN$,P,Q
   INPUT "DO YOU WANT TO CONTINUE?";CH$
   IF UCASE$(CH$)= "Y" THEN GOTO TOP
   CLOSE #1
   END
 

Q. WAP that asks item's name, rate and quantity and stores into 'sales.txt'. The user can supply 5 records in each execution.
4. OPEN "sales.txt" FOR OUTPUT AS #1
   FOR I = 1 TO 5
   CLS
   INPUT "ENTER ITEM'S NAME"; N$
   INPUT "ENTER RATE";R
   INPUT "ENTER QUANTITY";Q
   WRITE #1, N$,R,Q
   NEXT I
   CLOSE #1
   END


Q. WAP to add some more records in data file "sales.txt" where fields are item's name, rate and quantity.
5. OPEN "sales.txt" FOR APPEND AS #1
   TOP :
   CLS
   INPUT "ENTER ITEM'S NAME";N$
   INPUT "ENTER RATE";R
   INPUT "ENTER QUANTITY";Q
   WRITE #1, N$,R,Q
   INPUT "DO YOU WANT TO CONTINUE?";CH$
   IF UCASE$(CH$)="Y" THEN GOTO TOP
   CLOSE #1
   END

 
Q. WAP to store roll no, name and marks for three subjects in a sequential data file named "STUDENT.DAT".
6. OPEN "STUDENT.DAT" FOR OUTPUT AS #1
   TOP :
   CLS
   INPUT "ENTER ROLL NUMBER";RN
   INPUT "ENTER NAME"; N$
   INPUT "ENTER MARKS OF ANY THREE SUBJECTS";X,Y,Z
   WRITE #1,RN,N$,X,Y,Z
   INPUT "DO YOU WANT TO CONTINUE?";CH$
   IF UCASE$(CH$) = "Y" THEN GOTO TOP
   CLOSE #1
   END

 
Q. WAP to read all the records from the sequential data file "sales.txt" where rate is less than Rs. 750.
7. OPEN "sales.txt" FOR INPUT AS #1
   CLS
   WHILE NOT EOF(1)
   INPUT #1, N$,R,Q
   IF R<750 THEN PRINT N$,R,Q
   WEND
   CLOSE #1
   END


Q. A sequential data file "STUDENT.DAT" contains few records under the fields name, roll no and marks for three subjects. WAP to add few more records into the same data file.
8. OPEN "STUDENT.DAT" FOR APPEND AS #1
   TOP :
   CLS
   INPUT "ENTER ROLL NUMBER";RN
   INPUT "ENTER NAME"; N$
   INPUT "ENTER MARKS OF ANY THREE SUBJECTS";X,Y,Z
   WRITE #1,RN,N$,X,Y,Z
   INPUT "DO YOU WANT TO CONTINUE?";CH$
   IF UCASE$(CH$) = "Y" THEN GOTO TOP
   CLOSE #1
   END

 
Q. WAP to print all the records from the data file "STUDENT.DAT" where students are passed in all three subjects.
9. OPEN "STUDENT.DAT" FOR INPUT AS #1
   CLS
   WHILE NOT EOF(1)
   INPUT #1, RN,N$,X,Y,Z
   IF X>=40 AND Y>=40 AND Z>=40 THEN PRINT RN,N$,X,Y,Z
   WEND
   CLOSE #1
   END

 
Q. WAP to ask students' name, class and marks secured in three subjects. Store the data in sequential data file named "RESULT.DAT" along with total marks.
10.OPEN "RESULT.DAT" FOR OUTPUT AS #1
   TOP :
   CLS
   INPUT "ENTER NAME";N$
   INPUT "ENTER CLASS"";C
   INPUT "ENTER MARKS SECURED IN THREE SUBS"; X,Y,Z
   T = X+Y+Z
   WRITE #1, N$,C,X,Y,Z,T
   INPUT "DO YOU WANT TO CONTINUE?"; CH$
   IF UCASE$(CH$)="Y" THEN GOTO TOP
   CLOSE #1
   END
 

Q. A sequential data file "class.dat" has several records with fields students' name, roll no and class. WAP that reads all the data and displays only the records whose roll no is less than 15.
11.OPEN "class.dat" FOR INPUT AS #1
   CLS
   WHILE NOT EOF(1)
   INPUT #1, N$,R,C
   IF R<15 THEN PRINT N$,R,C
   WEND
   CLOSE #1
   END


Q. A sequential data file "EMP.DAT" contains name, post and salary fields of employees. WAP which displays all the records of the employee along with 10% of tax amount of salary.
12.OPEN "EMP.DAT" FOR INPUT AS #1
   CLS
   WHILE NOT EOF(1)
   INPUT #1,N$,P$,S
   T = 10/100*S
   PRINT N$,P$,S,T
   WEND
   CLOSE #1
   END

 
Q. A data file named "STUDENT.DAT" contains name, roll no, address and age of the students. WAP to display the records whose age is below 15.
13.OPEN "STUDENT.DAT" FOR INPUT AS #1
   CLS
   WHILE NOT EOF(1)
   INPUT #1, N$,R,A$,A
   IF A < 15 THEN PRINT N$,R,A$,A
   WEND
   CLOSE #1
   END

 
Q. A data file named "salary.dat" contains fields name, address and salary of some employees. WAP to read data from the data file and copy all the data to the new file named "nsalary.dat" where salary ranges from 15000 to 25000.
14.OPEN "salary.dat" FOR INPUT AS #1
   OPEN "nsalary.dat" FOR OUTPUT AS #1
   WHILE NOT EOF(1)
   INPUT #1, N$,A$,S
   IF S>15000 AND S<25000 THEN WRITE #2,N$,A$,S
   WEND
   CLOSE
   END
 

Q. WAP to search record according to the supplied name from the data file "RESULT.DAT" where fields are student's name, class and marks secured in three subjects.
16.OPEN "RESULT.DAT" FOR INPUT AS #1
   TOP:
   CLS
   FLAG = 0
   INPUT "ENTER NAME TO SEARCH";S$
   WHILE NOT EOF(1)
   INPUT #1,N$,C,X,Y,Z
   IF UCASE$(S$)=UCASE$(N$) THEN
   FLAG =1
   PRINT N$,C,X,Y,Z
   END IF
   WEND
   IF FLAG = 0 THEN PRINT "DATA NOT FOUND"
   INPUT "DO YOU WANT TO RETRY?"; CH$
   IF UCASE$(CH$)= "Y" THEN GOTO TOP
   CLOSE #1
   END

 
Q. WAP to delete unnecessary record from the data file named "STUDENT.DAT" according to the input name and display message if data is not found. The data file contains fields are student's name, address and class.
17.OPEN "STUDENT.DAT" FOR INPUT AS #1
   OPEN "TEMP.DAT" FOR OUTPUT AS #2
   TOP:
   CLS
   INPUT "ENTER NAME TO DELETE DATA"; D$
   FLAG = 0
   WHILE NOT EOF(1)
   INPUT #1,N$,A$,C
   IF UCASE$(N$) = UCASE$(D$) THEN
   PRINT "DATA DELETED"
   FLAG = 1
   ELSE
   WRITE #2,N$,A$,C
   END IF
   WEND
   IF FLAG = 0 THEN PRINT "DATA NOT FOUND"
   INPUT "DO YOU WANT TO DELETE ANOTHER DATA?";CH$
   IF UCASE$(CH$) = "Y" THEN GOTO TOP
   CLOSE
   KILL "STUDENT.DAT"
   NAME "TEMP.DAT" AS "STUDENT.DAT"
   END


Comments