Important SEE QBASIC file handling solutions.
OPEN “record.txt” FOR output AS #1
CLS
INPUT “Enter roll, name, address and class of a student”; r, n$, a$, c
WRITE #1, r, n$, a$, c
CLOSE #1
END
2. Write a program to create a sequential data file named "record.txt" and store Roll number, Name, Address and Class of few students.
OPEN “record.txt” FOR output AS #1
CLS
DO
INPUT “Enter roll, name, address and class of a student”; r, n$, a$, c
WRITE #1, r, n$, a$, c
INPUT “Do you want to continue”; x$
LOOP WHILE ucase$(x$) = “Y”
CLOSE #1
END
3. Write a program to add few more record in a data file named "record.txt" which contains Roll number, Name, Address and Class of few students.
OPEN “record.txt” FOR append AS #1
CLS
DO
INPUT “Enter roll, name, address and class of a student”; r, n$, a$, c
WRITE #1, r, n$, a$, c
INPUT “Do you want to add more”; x$
LOOP WHILE ucase$(x$) = “Y”
CLOSE #1
END
4. Write a program to display all the records from data file named "record.txt" which contains Roll number, Name, Address and Class of several students.
OPEN “record.txt” FOR input AS #1
CLS
PRINT "Roll No.", "Name", "Address", "Class"
WHILE NOT EOF(1)
INPUT #1, r, n$, a$, c
PRINT r, n$, a$, c
WEND
CLOSE #1
END
5. Write a program to display only those records whose name starts with "K" from data file named "record.txt" which contains Roll number, Name, Address and Class of several students.
OPEN “record.txt” FOR input AS #1
CLS
PRINT "Roll No.", "Name", "Address", "Class"
WHILE NOT EOF(1)
INPUT #1, r, n$, a$, c
z$ = left$(n$,1)
IF ucase(z$) = "K" THEN
PRINT r, n$, a$, c
END IF
WEND
CLOSE #1
END
6. Write a program to display only those records who lives in "POKHARA" from data file named "record.txt" which contains Roll number, Name, Address and Class of several students.
OPEN “record.txt” FOR input AS #1
CLS
PRINT "Roll No.", "Name", "Address", "Class"
WHILE NOT EOF(1)
INPUT #1, r, n$, a$, c
a$ = ucase$(a$)
IF a$ = "POKHARA" THEN
PRINT r, n$, a$, c
END IF
WEND
CLOSE #1
END
7. Write a program to count and display only those records who reads in class 10 from data file named "record.txt" which contains Roll number, Name, Address and Class of several students.
OPEN “record.txt” FOR input AS #1
CLS
PRINT "Roll No.", "Name", "Address", "Class"
WHILE NOT EOF(1)
INPUT #1, r, n$, a$, c
IF c = 10 THEN
PRINT r, n$, a$, c
x= x+1
END IF
WEND
PRINT "Total students who reads in class 10 are"; x
CLOSE #1
END
8. Write a program to display only those records whose roll number is in from 20 to 30 from data file named "record.txt" which contains Roll number, Name, Address and Class of several students.
OPEN “record.txt” FOR input AS #1
CLS
PRINT "Roll No.", "Name", "Address", "Class"
WHILE NOT EOF(1)
INPUT #1, r, n$, a$, c
IF r>=20 AND r<=30 THEN
PRINT r, n$, a$, c
END IF
WEND
CLOSE #1
END
9. Write a program to count only those records who doesn't lives in "POKHARA" from data file named "record.txt" which contains Roll number, Name, Address and Class of several students.
OPEN “record.txt” FOR input AS #1
CLS
PRINT "Roll No.", "Name", "Address", "Class"
WHILE NOT EOF(1)
INPUT #1, r, n$, a$, c
a$ = ucase$(a$)
IF a$ <> "POKHARA" THEN
x=x+1
END IF
WEND
PRINT "Total students who doesnot live in POKHARA are"; x
CLOSE #1
END
10. Write a program to delete all those records whose address is "KATHMANDU"from data file named "record.txt" which contains Roll number, Name, Address and Class of several students.
OPEN “record.txt” FOR input AS #1
OPEN "temp.txt" FOR output AS #2
CLS
PRINT "Roll No.", "Name", "Address", "Class"
WHILE NOT EOF(1)
INPUT #1, r, n$, a$, c
a$ = ucase$(a$)
IF a$ <> "KATHMANDU" THEN
WRITE #2, r, n$, a$, c
END IF
WEND
KILL "record.txt"
NAME "temp.txt" AS "record.txt"
CLOSE #1
CLOSE #2
END
Q) A sequential data file named "result.doc" contains Roll number, Name, Class and Marks obtained in 5 different subject i.e Maths, Science, English, Nepali, Computer. Let us consider Pass mark to be 40 and Full mark to be 100 then answer all the following questions.
OPEN “result.doc” FOR input AS #1
CLS
PRINT "Name"
WHILE NOT EOF(1)
INPUT #1, r, n$, c, m, s, e, n, co
IF m<40 OR s<40 OR e<40 OR n<40 OR co<40 THEN
PRINT n$
END IF
WEND
CLOSE #1
END
OPEN “result.doc” FOR input AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, r, n$, c, m, s, e, n, co
IF m>=40 AND s>=40 AND e>=40 AND n>=40 AND co>=40 THEN
x=x+1
END IF
WEND
PRINT "Total pass students are"; x
CLOSE #1
END
OPEN “result.doc” FOR input AS #1
CLS
PRINT "Roll", "Name", "Percentage"
WHILE NOT EOF(1)
INPUT #1, r, n$, c, m, s, e, n, co
t=m+s+e+n+co
p=t/5
PRINT r, n$, p
WEND
CLOSE #1
END
OPEN “result.doc” FOR input AS #1
CLS
PRINT "Name"
WHILE NOT EOF(1)
INPUT #1, r, n$, c, m, s, e, n, co
IF e<40 AND n<40 THEN
PRINT n$
END IF
WEND
CLOSE #1
END
OPEN “result.doc” FOR input AS #1
OPEN "temp.doc" FOR output AS #2
CLS
PRINT "Name"
WHILE NOT EOF(1)
INPUT #1, r, n$, c, m, s, e, n, co
IF m<40 OR s<40 OR e<40 OR n<40 OR co<40 THEN
ELSE
WRITE #2, r, n$, c, m, s, e, n, co
END IF
WEND
KILL "result.doc"
NAME "temp.doc" AS "result.doc"
CLOSE #1,#2
END