Displaying the Last Five Records of a Data File in QBASIC

2 years ago

Are you interested in programming and want to use QBASIC to handle files? This article will lead you through a straightforward QBASIC programme to retrieve and show the last five records from a data file called "student.txt." Information on students, including names, roll numbers, address, and class, can be found in the data file.

QBASIC is a versatile programming language that allows you to perform various file operations, including reading and writing data to files. In this example, we'll focus on reading data from a file and displaying the last five records.

Note: You should have the 'student.txt' data file in your QBASIC folder, from which you are running the QBASIC interpreter.

PROGRAM

    
    DIM Name(100) AS STRING
    DIM Roll(100) AS INTEGER
    DIM Address(100) AS STRING
    DIM Class(100) AS STRING
    DIM TotalRecords AS INTEGER

    OPEN "student.txt" FOR INPUT AS #1

    DO UNTIL EOF(1)
        TotalRecords = TotalRecords + 1
        LINE INPUT #1, Name(TotalRecords)
        LINE INPUT #1, Roll(TotalRecords)
        LINE INPUT #1, Address(TotalRecords)
        LINE INPUT #1, Class(TotalRecords)
    LOOP

    PRINT "Last Five Records:"
    FOR i = TotalRecords - 5 TO TotalRecords
        PRINT "Name: "; Name(i)
        PRINT "Roll: "; Roll(i)
        PRINT "Address: "; Address(i)
        PRINT "Class: "; Class(i)
        PRINT
    NEXT i

    CLOSE #1

    END
    
  805