File Handling
1. Write a program to store records regarding the information of students it includes information like roll number, name, class, gender, age and address in sequential data file “student.txt”.
OPEN “STUDENT.TXT” FOR OUTPUT AS #1
CLS
DO
INPUT
“ENTER ROLL, NAME, ADDRESS AND CLASS OF A STUDENT”; R, N$, C,G$, A,AD$
WRITE
#1, R, N$, C,G$, A,AD$
INPUT “DO YOU WANT TO CONTINUE”; X$
LOOP WHILE UCASE$(X$) = “Y”
CLOSE #1
END
2. A sequential data file “student.txt " contains
few records under the field’s roll number, name, class, gender, age and
address. Write a program to add few more records in the same sequential data
file.
OPEN “STUDENT.TXT” FOR APPEND AS #1
CLS
DO
INPUT
“ENTER ROLL, NAME, CLASS, GENDER AGE AND ADDRESS OF A STUDENT”; R, N$, C,G$,
A,AD$
WRITE
#1, R, N$, C,G$, A,AD$
INPUT
“DO YOU WANT TO CONTINUE”; X$
LOOP WHILE UCASE$(X$) = “Y”
CLOSE #1
END
3. A sequential data file "student.txt"
contains roll number, name, class, gender, age and address fields of
information about display all the information of students.
OPEN “STUDENT.TXT” FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT
#1, R, N$, C,G$, A,AD$
PRINT
R, N$, C,G$, A,AD$A, AD
WEND
CLOSE #1
END
4. A sequential data file "student.txt"
contains roll number, name, class, gender, age and address fields. WAP to count
and display information of students who studies in class 10 only.
OPEN “STUDENT.TXT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, R, N$, C,G$, A,AD$
IF C = 10 THEN
PRINT R, N$, C,G$, A,AD$
X= X+1
END IF
WEND
PRINT "TOTAL STUDENTS WHO READS IN CLASS 10
ARE"; X
CLOSE #1
END
5. A sequential data file "student.txt"
contains roll number, name, class, gender, age and address fields of
information about display information of students whose address is Kathmandu
only.
OPEN “STUDENT.TXT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, R, N$, C,G$, A,AD$
IF UCASE$(A$) = “KATHMANDU” THEN
PRINT R, N$, C,G$, A,AD$
END IF
WEND
CLOSE #1
END
6. A sequential data file “student.txt " has several
records having fields student's roll number, name, class, gender, age and
address. Write a program that reads all the records and displays only
those records whose name starts with 'K'.
OPEN “STUDENT.TXT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, R, N$, C,G$, A,AD$
Z$ = LEFT$(N$,1)
IF UCASE$(Z$) = "K" THEN
PRINT R, N$, C,G$, A,AD$
END IF
WEND
CLOSE #1
END
7. A sequential data file called "student.txt"
has stored data under the field heading roll number, name, class, gender, and
address. Write a program to display all the information of those students whose
gender are "F" and age is more than 14.
OPEN “STUDENT.TXT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT
#1, R, N$, C,G$, A,AD$
IF
UCASE$(G$) = “F” AND A>14 THEN
PRINT R, N$, C,G$, A,AD$
END
IF
WEND
CLOSE #1
END
8. 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
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
9. A sequential data file “class.dat” has several records
having field roll number, name, class, gender, age and address. WAP to copy all
the records of class 10 into newly record file “new.dat.”
OPEN "class.dat" FOR INPUT AS #1
OPEN "new.dat" FOR OUTPUT AS #2
WHILE NOT EOF(1)
INPUT
#1, roll, name$, class, gender$, age, address$
IF
class = 10 THEN
WRITE #2, roll, name$, class, gender$, age, address$
END
IF
WEND
CLOSE #1
CLOSE #2
END
10. Write a program to calculate and display percentage of
all the students along with the roll number and name.
OPEN "marks.dat" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, roll, name$, m1, m2, m3, m4, m5
total
= m1 + m2 + m3 + m4 + m5
percentage
= total / 5
PRINT
roll, name$, percentage
WEND
CLOSE #1
END
11. Write a qbasic program to read all the records from
the data file "record.txt" containing name and salary. Then find and
display the lowest, highest and average salary of the company.
OPEN "RECORD.TXT" FOR INPUT AS #1
INPUT #1, N$, SAL
S = SAL
H = SAL
CLOSE #1
OPEN "RECORD.TXT" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, N$, SAL
PRINT N$, SAL
IF S > SAL THEN
S = SAL
END IF
IF H < SAL THEN
H = SAL
END IF
SUM = SUM + SAL
N = N + 1
WEND
AVE = SUM / N
PRINT AVE
PRINT S
PRINT H
CLOSE #1
END
1.
Opening the file
OPEN
"RECORD.TXT" FOR INPUT AS #1
·
Opens
the file RECORD.TXT for reading.
·
#1
is the file number used to access the file.
2.
Reading the first record
INPUT
#1, N$, SAL
·
Reads
Name (N$) and Salary (SAL) from the file.
3.
Initializing variables
S
= SAL
H = SAL
·
S
stores the smallest salary (initially first salary).
·
H
stores the highest salary (initially first salary).
·
The
first value is used as a starting reference.
4.
Closing and reopening the file
CLOSE
#1
OPEN "RECORD.TXT" FOR INPUT AS #1
·
The
file is reopened to start reading again from the beginning.
5.
Loop until end of file
WHILE
NOT EOF(1)
·
EOF(1)
means End Of File.
·
The
loop continues until all records are read.
6.
Reading each record
INPUT
#1, N$, SAL
PRINT N$, SAL
·
Reads
name and salary.
·
Displays
them on the screen.
7.
Finding the lowest salary
IF
S > SAL THEN
S = SAL
END IF
·
If
the current salary is less than the stored smallest salary, update S.
8.
Finding the highest salary
IF
H < SAL THEN
H = SAL
END IF
·
If
the current salary is greater than the stored highest salary, update H.
9.
Calculating total and counting records
SUM
= SUM + SAL
N = N + 1
·
Adds
salary to total sum.
·
N
counts the number of records.
10.
End of loop
WEND
·
Ends
the WHILE loop.
11.
Calculating average salary
AVE
= SUM / N
·
Computes
average salary.
12.
Displaying results
PRINT
AVE
PRINT S
PRINT H
Prints:
·
Average salary
·
Smallest salary
·
Highest salary
13.
Closing file
CLOSE
#1
END
·
Closes
the file and ends the program.
12. SEDIP
2081-A SEQUENTIAL DATAFILE “teacher.txt” CONTAINS 20 TEACHERS NAME. WAP TO
DISPLAY NAME IN REVERSE FORM.
OPEN
"TEACHER.TXT" FOR INPUT AS #1
CLS
FOR
I = 1 TO 20
LINE INPUT #1, N$
Rev$ = ""
FOR J = LEN(N$) TO 1 STEP -1
Rev$ = Rev$ + MID$(N$, J, 1)
NEXT J
PRINT Rev$
NEXT
I
CLOSE
#1
END
1.
Open the file
OPEN
"TEACHER.TXT" FOR INPUT AS #1
Opens
the file TEACHER.TXT for reading.
·
#1
is the file number used to access the file.
2.
Loop to read 20 names
FOR
I = 1 TO 20
·
The
loop runs 20 times, meaning it reads 20 teacher names from the file.
3.
Read a line from the file
LINE
INPUT #1, N$
·
Reads
a full line (teacher name) from the file and stores it in N$.
4.
Initialize reverse string
Rev$
= ""
·
Creates
an empty string to store the reversed name.
5.
Reverse the name
FOR
J = LEN(N$) TO 1 STEP -1
Rev$ = Rev$ + MID$(N$, J, 1)
NEXT J
LEN(N$)
gets the length of the name.
The
loop starts from the last character to the first.
MID$(N$,
J, 1) extracts one character at position J.
Each
character is added to Rev$, creating the reverse string.
Example:
If N$ = "RAM"
Output becomes "MAR".
6.
Print the reversed name
PRINT
Rev$
Displays
the reversed teacher name.
13. A sequential data file called "staff.txt" has stored data under the field headings: Name, address , post and date of birth (MM-DD-YYYY). Write a program to display all the information of those employees who were born between the years 1970 AD to 2020 AD.
Open "staff.txt" for input as #1
While not eof (1)
Input
#1, n$,a$, p$, dob$
X$ =
Right$(DOB$, 4)
d =
val(X$)
If
d>=1970 and d<=2020 then
Print n$,a$, p$, dob$
End
if
Wend
close #1
End