Sunday, 29 December 2024

Grade-10 Ch:13 File Handling Lab Work RoseBud

 

Lab Questions and Solutions on File Handling in QBASIC


Question 1: File Handling with OUTPUT, APPEND, and Conditional INPUT Modes

Problem Statement:
Write a QBASIC program that performs the following tasks:

  1. Creates a file named Students.txt and writes the details of 5 students (name and marks) using the OUTPUT mode.
  2. Appends new student details (name and marks) to the file using the APPEND mode.
  3. Reads the details of students who scored more than 50 marks from the file using the INPUT mode and displays them on the screen.

Solution Code:

REM Step 1: Writing to file using OUTPUT mode
OPEN "Students.txt" FOR OUTPUT AS #1
FOR i = 1 TO 5
    PRINT "Enter the name of student "; i; ": ";
    INPUT name$
    PRINT "Enter marks for "; name$; ": ";
    INPUT marks
    WRITE #1, name$, marks
NEXT i
CLOSE #1



REM Step 2: Appending new data using APPEND mode
OPEN "Students.txt" FOR APPEND AS #1
PRINT "Do you want to add another student? (Y/N): ";
INPUT choice$
IF UCASE$(choice$) = "Y" THEN
    PRINT "Enter the name of new student: ";
    INPUT name$
    PRINT "Enter marks for "; name$; ": ";
    INPUT marks
    WRITE #1, name$, marks
END IF
CLOSE #1


REM Step 3: Reading from file using INPUT mode with condition
OPEN "Students.txt" FOR INPUT AS #1
PRINT "Students with marks greater than 50:"
DO WHILE NOT EOF(1)
    INPUT #1, name$, marks
    IF marks > 50 THEN
        PRINT name$; " - "; marks
    END IF
LOOP
CLOSE #1

Question 2:  QBASIC program to store data into a file student.txt, containing the fields name, age, roll, and marks.


Program Code

REM Open the file in OUTPUT mode
OPEN "student.txt" FOR OUTPUT AS #1

PRINT "Enter student details (name, age, roll, marks). Type 'STOP' as name to finish."

DO
    REM Input student details
    PRINT "Enter student name (or type 'STOP' to end): ";
    INPUT name$
    
    PRINT "Enter age: ";
    INPUT age
    
    PRINT "Enter roll number: ";
    INPUT roll
    
    PRINT "Enter marks: ";
    INPUT marks
    
    REM Write the student details to the file
    WRITE #1, name$, age, roll, marks
LOOP

REM Close the file
CLOSE #1

PRINT "Student details have been saved in 'student.txt'."

Explanation of the Code

  1. File Creation:

    • The OPEN "student.txt" FOR OUTPUT AS #1 statement creates the file student.txt if it does not exist, or overwrites it if it already exists.
  2. User Input:

    • The program continuously prompts the user to enter student details (name, age, roll, marks).
    • If the user types "STOP" for the name, the loop exits.
  3. Data Storage:

    • The WRITE #1 statement saves the data in the file in a structured format.
  4. Loop Control:

    • The DO...LOOP structure ensures that multiple student records can be entered.
  5. File Closure:

    • The CLOSE #1 statement ensures all data is written to the file and properly closes it.


QUESTION 3: Write a program in QBAISC to store and rename a data file "temp.txt" to "student.txt" from a file "student.txt" in a field name, age, roll, marks use KILL and NAME statement to delete file "student.txt" and NAME statement to rename "temp.txt" to "student.txt".Program Code

REM Step 1: Create and write data to temp.txt
OPEN "temp.txt" FOR OUTPUT AS #1
OPEN"student.txt" FOR INPUT AS#2

WHILE NOT EOF(2)
    INPUT #2,name$, age, roll, marks
    WRITE #1, name$,age, roll, marks
 
WEND

CLOSE #1, CLOSE#2
PRINT "Data has been written to 'temp.txt'."

    KILL "student.txt"
    PRINT "'student.txt' has been deleted."
NAME "temp.txt" AS "student.txt"
PRINT "'temp.txt' has been renamed to 'student.txt'."

PRINT "Operation completed successfully."
END

Explanation of the Code

  1. Creating and Writing to temp.txt:

    • The program creates temp.txt and stores the student data (fields: name, age, roll, marks) using the OPEN "temp.txt" FOR OUTPUT statement.
  2. Deleting student.txt:

    • If the file exists, the KILL statement deletes it.
  3. Renaming temp.txt to student.txt:

    • The NAME "temp.txt" AS "student.txt" statement renames the temporary file to student.txt.
  4. Ensuring Completion:

    • Messages are displayed to the user, confirming each step of the operation.


No comments:

Post a Comment

Popular Posts