QBASIC |File Handling| Examples Grade 8, 9, 10


If you are new, Start from here: Intro to Qbasic

Introduction

One of the essential features of QBasic is its file handling capabilities. File handling refers to the ability of a programming language to read from and write to external files on a computer's disk. QBasic provides several commands and functions to perform various file operations, such as creating, opening, reading, writing, and closing files.

Here's a brief introduction to the file handling concepts in QBasic:

1. File Types: QBasic supports two types of files: sequential files and random access files. Sequential files are read or written sequentially from the beginning to the end, while random access files allow direct access to any part of the file.

2. File Operations: To perform file operations, QBasic provides commands like OPEN, CLOSE, WRITE, INPUT, PRINT, GET, and PUT. These commands allow you to open files, close files, read data from files, write data to files, and manipulate the file pointer.

3. File Modes: When opening a file, you can specify the file mode, which determines how the file is accessed. QBasic supports various file modes such as INPUT, OUTPUT, APPEND, RANDOM, and BINARY. Each mode provides different functionality for reading and writing files.

4. File Handles: QBasic uses file handles to keep track of open files. A file handle is a numeric value that represents an open file. You can assign a file handle using the OPEN statement and use it to refer to the file in subsequent file operations.

5. Error Handling: QBasic provides error handling mechanisms for file operations. You can check for errors during file operations using the ERROR statement or the ERR function. These help you handle situations where a file operation fails or encounters an error.

File handling in QBasic allows you to read data from existing files, write data to new or existing files, create databases, process large amounts of information, and much more. Understanding and utilizing file handling capabilities in QBasic is essential for building programs that work with external data and files.

Lets Start with practice Examples: 

1.Creating a New Text File and Writing Data:

OPEN "data.txt" FOR OUTPUT AS #1    
PRINT #1, "Hello, World!"
PRINT #1, "This is a sample text file."
CLOSE #1

2.Reading Data from an Existing Text File:

OPEN "data.txt" FOR INPUT AS #1
DO UNTIL EOF(1)
    LINE INPUT #1, textLine
    PRINT textLine
LOOP
CLOSE #1

3.Appending Data to an Existing Text File:

OPEN "data.txt" FOR APPEND AS #1
PRINT #1, "Additional line of text."
CLOSE #1

4.Writing and Reading Binary Data:

TYPE Person
    Name AS STRING * 20
    Age AS INTEGER
END TYPE

DIM person AS Person
person.Name = "John Smith"
person.Age = 30

OPEN "data.bin" FOR BINARY AS #1
PUT #1, , person
CLOSE #1

OPEN "data.bin" FOR BINARY AS #1
GET #1, , person
CLOSE #1

PRINT person.Name
PRINT person.Age

5.Random Access File Operations:

TYPE Employee
    Name AS STRING * 20
    Salary AS SINGLE
END TYPE

DIM employee AS Employee

OPEN "data.dat" FOR RANDOM AS #1 LEN = LEN(employee)

employee.Name = "John Smith"
employee.Salary = 5000

PUT #1, 1, employee

GET #1, 1, employee
PRINT employee.Name
PRINT employee.Salary

CLOSE #1

6.Copying the Contents of One Text File to Another:

OPEN "input.txt" FOR INPUT AS #1
OPEN "output.txt" FOR OUTPUT AS #2

DO UNTIL EOF(1)
    LINE INPUT #1, textLine
    PRINT #2, textLine
LOOP

CLOSE #1
CLOSE #2

7.Reading Numbers from a Text File and Calculating Their Sum:

OPEN "numbers.txt" FOR INPUT AS #1
total = 0

DO UNTIL EOF(1)
    INPUT #1, number
    total = total + number
LOOP

PRINT "Sum:", total

CLOSE #1

8.Searching for a Specific Line in a Text File:

OPEN "data.txt" FOR INPUT AS #1
lineNumber = 0
targetLine = "Hello, World!"

DO UNTIL EOF(1)
    lineNumber = lineNumber + 1
    LINE INPUT #1, textLine
    IF textLine = targetLine THEN
        PRINT "Line found at:", lineNumber
        EXIT DO
    END IF
LOOP

CLOSE #1

9.Deleting a File:.

KILL "file.txt"

10.Checking if a File Exists:

IF FILEATTR("data.txt") <> -1 THEN
    PRINT "File exists."
ELSE
    PRINT "File does not exist."
END IF


I Suggest Students should learn the basic from their books and then practice this examples using their own way of programming. i mean see this examples and use your own creativity to create your programs. By doing that your programming skill will improved and it will lead tou to better understand of codes i hope.  



if you found this blog informative and useful then please share with your friends. happy coding.
Thank You....!



Post a Comment

Previous Post Next Post