FILE HANDLING (SOLVED)
1. Write a QBASIC program to create a file named including the name, class, and roll number of a student.
OPEN "std.dat" FOR OUTPUT AS #1
CLS
INPUT "Enter student name: ", name$
INPUT "Enter student class: ", class$
INPUT "Enter roll number: ", roll$
WRITE #1, name$, class$, roll$
CLOSE #1
PRINT "Student data saved successfully in std.dat!"
END
2. Write program to read and display the name, class and roll number of students.
CLS
OPEN "std.dat" FOR INPUT AS #1
DO WHILE NOT EOF(1)
INPUT #1, name$, class$, roll$
PRINT "Name: "; name$; ", Class: "; class$; ", Roll No.: "; roll$
LOOP
CLOSE #1
END
3. Read data from a file and calculate total marks:
CLS
OPEN "marks.dat" FOR INPUT AS #1
total = 0
DO WHILE NOT EOF(1)
INPUT #1, marks
total = total + marks
LOOP
PRINT "Total Marks: "; total
CLOSE #1
END
4. Search for a specific student's data in:
CLS
OPEN "records.dat" FOR INPUT AS #1
INPUT "Enter name to search: ", searchName$
found = 0
DO WHILE NOT EOF(1)
INPUT #1, name$, details$
IF name$ = searchName$ THEN
PRINT "Details: "; details$
found = 1
END IF
LOOP
IF found = 0 THEN PRINT "Student not found!"
CLOSE #1
END
5. Count and display the number of records in a file:
CLS
OPEN "students.dat" FOR INPUT AS #1
count = 0
DO WHILE NOT EOF(1)
LINE INPUT #1, data$
count = count + 1
LOOP
PRINT "Total Records: "; count
CLOSE #1
END
6. Display specific fields from (e.g., names only):
CLS
OPEN "data.dat" FOR INPUT AS #1
DO WHILE NOT EOF(1)
INPUT #1, name$, info$
PRINT "Name: "; name$
LOOP
CLOSE #1
END
7. Write a QBASIC program to create a file named including the name, class, and roll number of a student.
CLS
OPEN "std.dat" FOR OUTPUT AS #1
INPUT "Enter student name: ", name$
INPUT "Enter student class: ", class$
INPUT "Enter roll number: ", roll$
WRITE #1, name$, class$, roll$
CLOSE #1
PRINT "Student data saved successfully in std.dat!"
END
8. Write a QBASIC program to create a text file named . The file should store the names and marks of five students entered by the user.
OPEN "StudentInfo.txt" FOR OUTPUT AS #1
CLS
FOR i = 1 TO 5
INPUT "Enter student name: ", name$
INPUT "Enter student marks: ", marks
WRITE #1, name$, marks
NEXT i
CLOSE #1
PRINT "Data written to StudentInfo.txt successfully!"
END
9. Write a QBASIC program to read and display the contents of a text file named . Assume the file contains student names and grades.
CLS
OPEN "Grades.txt" FOR INPUT AS #1
DO WHILE NOT EOF(1)
INPUT #1, name$, grade$
PRINT "Name: "; name$; ", Grade: "; grade$
LOOP
CLOSE #1
END
10. Write a QBASIC program that appends new records (names and scores) to an existing file called without overwriting its current contents.
CLS
OPEN "Results.txt" FOR APPEND AS #1
INPUT "Enter name: ", name$
INPUT "Enter score: ", score
WRITE #1, name$, score
CLOSE #1
PRINT "Record added to Results.txt successfully!"
END
11. Write a QBASIC program to count and display the total number of lines present in a text file named.
CLS
OPEN "Data.txt" FOR INPUT AS #1
lineCount = 0
DO WHILE NOT EOF(1)
LINE INPUT #1, text$
lineCount = lineCount + 1
LOOP
PRINT "Total number of lines: "; lineCount
CLOSE #1
END
12. A sequential data file called "STUDENT.DAT" has stored data under the field heading Registration No., Class, Section, Name, Date of Birth and Gender. Write a program to display all the information of class NINE students whose gender is 'FEMALE'.
OPEN “STUDENT.DAT” FOR INPUT AS #1
CLS
WHILE NOT EOF(1)
INPUT #1, R, CL, SE$, N$, D$, G$
IF UCASE$(G$)=”FEMALE” AND CL=9 THEN
PRINT R, CL, SE$, N$, D$, G$
END IF
WEND
CLOSE #1
END
No comments:
Post a Comment