FILE HANDLING IN QBASIC (EXERCISE 3)



#WRITE A PROGRAM TO OPEN A DATA FILE “STUDENT.DAT” THAT CONTAINS NAME, ADDRESS, TELEPHONE NUMBER AND PATENTS NAME OF SOME STUDENTS. NOE, DISPLAY ALL THE RECORDS WHOSE ADDRESS IS “LALITPUR”.

CLS

OPEN “STUDENT.DAT” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, N$,A$,T#,P$

IF UCASE$(A$)=”LALITPUR” THEN

PRINT N$,A$,T#,P$

END IF

WEND

CLOSE #1

END

 

#A SEQUENTIAL DATA FILE “STD.REC” CONTAINS RECORDS OF STUDENTS NAME, ROLL NUMBER, SECTION AND ADDRESS OF 10 STUDENTS. WRITE A PROGRAM TO DISPLAY THE LAST FIVE RECORDS.

CLS

OPEN “STD.REC” FOR INPUT AS #1

FOR I=1 TO 10

INPUT #1, N$,R,S$,AD$

IF I>S THEN

PRINT N$,R,S$,AD$

END IF

NEXT I

CLOSE #1

END

 

#A SEQUENTIAL DATA FILE “SALARY.DAT” CONTAINS INFORMATION AS EMPLOYEE CODE, EMPLOYEE NAME, POST AND BASIC SALARY. WRITE A PROGRAM TO DISPLAY THOSE RECORDS WHOSE BASIC SALARY IS BETWEEN 10000 TO 15000 AND POST IS “OFFICER”.

CLS

OPEN “SALARY.DAT” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1,EC,EN$,P$,S

IF S>=10000 AND S<=15000 AND UCASE$(P$)=”OFFICER” THEN

PRINT EC,EN$,P$,S

END IF

WEND

CLOSE #1

END

 

#WRITE A PROGRAM TO DISPLAY ALL THE RECORDS FROM A SEQUENTIAL DATA FILE “ABC.DAT”.

CLS

OPEN “ABC.DAT” FOR INPUT AS #1

WHILE NOT EOF(1)

LINE INPUT #1, S$

PRINT S$

WEND

CLOSE #1

END

 

#A DATA FILE NAMED “STAFF.DAT” CONTAINS STAFF NAME, DEPARTMENT, POST AND SALARY OF SOME STAFFS. WRITE A PROGRAM TO COUNT AND DISPLAY TOTAL NUMBER OF RECORDS IN A FILE.

CLS

OPEN “STAFF.DAT” FOR INPUT AS #1

C=0

WHILE NOT EOF(1)

INPUT #1, N$,D$,P$,S

C=C+1

PRINT N$,D$,P$,S

WEND

PRINT “TOTAL NUMBER OF RECORDS=”;C

CLOSE #1

END

 

#WRITE A PROGRAM TO RETRIEVE RECORDS OF STUDENTS FROM DATA FILE.

CLS

OPEN “STD.DAT” FOR INPUT AS #1

PRINT “ROLL”, “NAME”, “CLASS”

WHILE NOT EOF(1)

INPUT #1, R,N$,C

PRINT R,N$,C

WEND

CLOSE #1

END

 

#A DATA FILE NAMED “RECORD.DAT” CONTAINS NAME, POST AND SALARY FOR ‘N’ NUMBER OF PERSON. WRITE A PROGRAM TO INPUT A NAME TO SEARCH DATA FROM DATA FILE. IF DATA IS NOT FOUND, THEN PRINT “DATA IS NOT IN THE LIST.

CLS

OPEN “RECORD.DAT” FOR INPUT AS #1

INPUT “ENTER NAME OF PERSON TO SEARCH DATA”;N1$

FLAG=0

DO

INPUT #1, N$,P$,S

IF N1$=N$ THEN

PRINT N$,P$,S

FLAG=1

EXIT DO

END IF

LOOP WHILE NOT EOF(1)

IF FLAG=0 THEN

PRINT “DATA IS NOT FOUND IN THE LIST”

END IF

CLOSE #1

END

 

#A SEQUENTIAL DATA FILE “STAFF.INF” CONTAINS RECORDS OF SOME STAFFS WITH STAFF’S NAME, POST, SALARY AND DATE OF JOIN FIELD. WRITE A PROGRAM TO: (i)DISPLAY RECORDS OF THOSE STAFF WHOSE NAME START WITH K. (ii)DISPLAY RECORDS OF THOSE STAFF WHO HAD JOINED BEFORE 2019 AD.

(i)

CLS

OPEN “STAFF.INF” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, A$,B$,C,D

IF LEFT$(A$,1)=”K” THEN

PRINT A$,B$,C,D

WEND

CLOSE #1

END

(ii)

CLS

OPEN “STAFF.INF” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, N$,P$,S,DOJ$

YEAR$=RIGHT$(DOJ$,4)

IF VAL(YEAR$)<2019 THEN

PRINT N$,P$,S,DOJ$

END IF

WEND

CLOSE #1

END

 

#A SEQUENTIAL DATA FILE “EXAM.INF” CONTAINS SYMBOL NUMBER, STUDENTS’ NAME AND MARKS SECURED BY STUDENTS IN MATHS, SCIENCE AND ENGLISH SUBJECTS. WRITE A PROGRAM TO READ THE RECORDS OF “EXAM.INF” AND PERFORM THE FOLLOWING TASKS:

(i)DISPLAY ALL THE RECORDS OF STUDENTS.

(ii)DISPLAY A LIST OF RECORDS WITH SYMBOL NUMBER, STUDENT NAME AND TOTAL MARKS (SUM OF MARKS OF 3 SUBJECTS) AND PERCENTAGE.

(iii)COUNT AND DISPLAY RECORDS OF STUDENTS WHO HAS SECURED MORE THAN 80 IN MATH.

(iv)DISPLAY THE RECORDS OF PASS STUDENT WITH SYMBOL NUMBER, STUDENT NAME AND PERCENTAGE. ASSUME PASS MARK FOR EACH SUBJECT TO BE 32.

(v)DISPLAY RECORDS OF ONLY FAILED STUDENTS. ASSUME PASS MARK FOR EACH SUBJECT TO BE 32.

ANS:

(i)CLS

OPEN “EXAM.INF” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, S,SN$,M,S,E

PRINT S,SN$,M,S,E

WEND

CLOSE #1

END

(ii)

CLS

OPEN “EXAM.INF” FOR INPUT  AS #1

WHILE NOT EOF(1)

INPUT #1, S,SN$,M,S,E

TM=M+S+E

P=TM/3

PRINT S,SN$,TM,P

WEND

CLOSE #1

END

(iii)

CLS

OPEN “EXAM.INF” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, S,SN$,M,S,E

IF M>80 THEN

C=C+1

PRINT S,SN$,M,S,E

END IF

WEND

PRINT “TOTAL NUMBER OF STUDENTS MORE THAN 80 IN MATHS”;C

CLOSE #1

END

(iv)

CLS

OPEN “EXAM.INF” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, S,SN$,M,S,E

IF M>32 AND S>32 AND E>32 THEN

P=(M+S+E)/3

PRINT S,SN$,P

END IF

WEND

CLOSE #1

END

(v)

CLS

OPEN “EXAM.INF” FOR INPUT AS #1

WHILE NOT EOF(1)

INPUT #1, S,SN$,M,S,E

IF M<32 AND S<32 AND E<32 THEN

PRINT S,SN$,M,S,E

END IF

WEND

CLOSE #1

END

 

#A SEQUENTIAL DATA FILE CALLED “STUDENT.DAT” CONTAINS SOME RECORD UNDER THE FILEDS: NAME, ENGLISH, NEPALI AND COMPUTER. WRITE A PROGRAM TO ADD SOME MORE RECORDS IN THE SAME SEQUENTIAL FILE.

CLS

OPEN “STUDENT.DAT” FOR APPEND AS #1

DO

INPUT “ENTER THE NAME”;N$

INPUT “ENTER MARKS OF ENGLISH”;E

INPUT “ENTER MARKS OF NEPALI”;N

INPUT “ENTER MARKS OF COMPUTER”;C

WRITE #1, N$,E,N,C

INPUT “DOYOU WANT TO CONTINUE (Y/N)”;ANS$

LOOP WHILE UCASE$(ANS$)=”Y”

CLOSE #1

END

 

#A SEQUENTIAL DATA FILE “AVN.DAT” CONTAINS SOME INFORMATION OF EMPLOYEES UNDER THE FIELDS NAME, ADDRESS, TELEPHONE NUMBER, POST, DEPARTMENT AND AGE. WRITE A PROGRAM TO ADD SOME MORE RECORDS TO THE SAME DATA FILE.

CLS

OPEN “AVN.DAT” FOR APPEND AS #1

DO

INPUT “ENTER NAME”;N$

INPUT “ENTER ADDRESS”A$

INPUT “ENTER TELEPHONE NUMBER”;T#

INPUT “ENTER POST”;P$

INPUT “ENTER DEPARTMENT”;D$

INPUT “ENTER AGE”;A

WRITE #1, N$,A$,T#,P$,D$,A

INPUT “DO YOU WANT TO CONTINUE (Y/N)”;ANS$

LOOP WHILE UCASE$(ANS$)= “Y”

CLOSE #1

END

 

#A SEQUENTIAL DATA FILE “INFO.DAT” CONTAINS NAME, ADDRESS, AGE AND GENDER OF SOME PEOPLE. WRITE A PROGRAM TO ADD 10 MORE RECORDS IN THE FILE.

CLS

OPEN “INFO.DAT” FOR APPEND AS #1

FOR I=1 TO 10

INPUT “ENTER NAME”;N$

INPUT “ENTER ADDRESS”;A$

INPUT “ENTER AGE”;A

INPUT “ENTER GENDER”;G$

WRITE #1, N$,A$,A,G$

NEXT I

CLOSE #1

END

 

#WRITE A PROGRAM TO ADD NEW RECORDS IN THE EXISTING DATA FILE.

CLS

OPEN “STD.DAT” FOR APPEND AS #1

DO

INPUT “ENTER ROLL NUMBER”;R

INPUT “ENTER NAME”;N$

INPUT “ENTER CLASS”;C

WRITE #1, R,N$,C

INPUT “DO YOU WANT TO CONTINUE (Y/N)”;ANS$

LOOP WHILE UCASE$(ANS$)=”Y”

CLOSE #1

END

 

#WRITE A MENU DRIVEN QBASIC PROGRAM TO HANDLE DIFFERENT CASES.

CASE 1: TO ENTER DATA THAT REFER TO NAME, ADDRESS AND TELEPHONE NUMBER FOR 20 PERSONS TO A SEQUENTIAL DATA FILE “ADDRESS.DAT”.

CASE 2: TO DISPLAY THE RECORDS OF “ADDRESS.DAT” DATA FILE.

CASE 3: TO QUIT

ANS:

CLS

PRINT DATE$; TAB(20); ”MENU LIST”

PRINT

PRINT “1. TO ENTER DATA”

PRINT “2. TO DISPLAY RECORDS”

PRINT “3. TO QUIT”

INPUT “ENTER YOUR CHOICE”;CHOICE

SELECT CASE CHOICE

CASE 1:

OPEN “ADDRESS.DAT” FOR OUTPUT AS #1

FOR I=1 TO 20

INPUT “ENTER NAME”;N$

INPUT “ENTER ADDRESS”;A$

INPUT “ENTER TELEPHONE NUMBER”;T#

WRITE #1, N$,A$,T#

NEXT I

CLOSE #1

CASE 2:

OPEN “ADDRESS.DAT” FOR INPUT AS #1

FOR I=1 TO 20

INPUT #1, N$,A$,T#

PRINT N$,A$,T#

NEXT I

CLOSE #1

CASE 3:

END

END SELECT

END

Sujit Prasad Kushwaha

A Dedicated Blogger Sharing Insights and Making a Difference.

Post a Comment

The comment section is your chance to make your mark. Don't hold back - add to the discussion and let your personality shine through!

Previous Post Next Post