File Handling in QBASIC
File handling is a vital aspect of programming, allowing the programmer to read from and write data to files. In QBASIC (Quick Beginners All-Purpose Symbolic Instruction Code), file handling is straightforward and involves the basic operations of creating, reading, updating, and deleting files. This functionality helps in managing data storage for various applications like keeping records, processing text files, or managing data files.
Table of Contents
ToggleModes of File Handling in QBASIC
QBASIC supports several modes for file handling, each with specific purposes:
- OUTPUT – For writing data to a new file or overwriting an existing file.
- INPUT – For reading data from a file.
- APPEND – For adding data to the end of an existing file without overwriting its contents.
- RANDOM – For accessing data randomly in a file.
- BINARY – For handling binary files.
File Handling Operations in QBASIC
1. Opening a File
To perform any file operation, you must first open the file using the OPEN
statement. The basic syntax of the OPEN
statement is:
OPEN "filename" FOR mode AS #fileNumber
"filename"
refers to the name of the file you want to open.mode
is the mode in which you are opening the file (INPUT
,OUTPUT
,APPEND
,RANDOM
, orBINARY
).#fileNumber
is an arbitrary number (from 1 to 255) that you assign to the file. It is used to refer to the file in subsequent file operations.
Example:
OPEN "data.txt" FOR OUTPUT AS #1
In this example, the file data.txt
is opened for writing, and the file number is set to 1
.
2. Writing to a File
To write data into a file, you use the PRINT #
statement. Data is written into the file as text or in the format you specify.
PRINT #fileNumber, data
Example:
OPEN "data.txt" FOR OUTPUT AS #1
PRINT #1, "Hello, this is QBASIC!"
CLOSE #1
Here, the program opens data.txt
for output, writes a line of text into it, and then closes the file using the CLOSE
statement.
3. Reading from a File
To read from a file, the file must be opened in INPUT
mode, and you use the INPUT #
or LINE INPUT #
statement to read data from the file.
INPUT #fileNumber, variable
Example:
OPEN "data.txt" FOR INPUT AS #1
INPUT #1, text$
PRINT text$
CLOSE #1
In this example, the program reads the contents of the file data.txt
and prints it on the screen.
4. Appending to a File
To add new data to an existing file without overwriting its contents, open the file in APPEND
mode. Data will be written at the end of the file.
Example:
OPEN "data.txt" FOR APPEND AS #1
PRINT #1, "This is an appended line."
CLOSE #1
5. Closing a File
After performing any operation on a file, it is essential to close it using the CLOSE
statement. This releases the file so other programs or processes can access it if needed.
CLOSE #fileNumber
6. Random Access Files
Random access files allow you to read or write records directly without reading the entire file. In QBASIC, random access file handling uses the RANDOM
mode. Each record is typically assigned a specific number, allowing random access.
The FIELD
statement is used to define the size of each record, and GET
and PUT
statements are used for reading and writing records.
Syntax:
OPEN "file.dat" FOR RANDOM AS #1 LEN = length
FIELD #1, fieldLength AS variableName
GET #1, recordNumber
reads a specific record from the file.PUT #1, recordNumber
writes a record to the file.
Example:
TYPE Employee
ID AS INTEGER
Name AS STRING * 20
Age AS INTEGER
END TYPE
DIM employee AS Employee
OPEN "employee.dat" FOR RANDOM AS #1 LEN = LEN(employee)
FIELD #1, 4 AS ID, 20 AS Name$, 2 AS Age
' Writing a record
employee.ID = 1
employee.Name = "John Doe"
employee.Age = 30
PUT #1, 1, employee
' Reading the record
GET #1, 1, employee
PRINT employee.ID, employee.Name, employee.Age
CLOSE #1
Important File Handling Statements in QBASIC
- OPEN: Opens a file in a specific mode.
- CLOSE: Closes an open file.
- PRINT #: Writes data to a file.
- INPUT #: Reads data from a file.
- LINE INPUT #: Reads an entire line of data from a file.
- GET: Retrieves a record from a random-access file.
- PUT: Writes a record to a random-access file.
- FIELD: Assigns buffer space for random file fields.
Handling Errors in File Operations
To ensure that file operations do not cause runtime errors (e.g., trying to read a non-existent file), you can use ON ERROR
statements to define error-handling routines.
Example:
ON ERROR GOTO ErrorHandler
OPEN "nonexistent.txt" FOR INPUT AS #1
INPUT #1, data$
PRINT data$
CLOSE #1
END
ErrorHandler:
PRINT "Error: Unable to open the file!"
RESUME NEXT
File handling in QBASIC is simple and essential for many applications, from basic text processing to managing records in random-access files. Understanding how to use the OPEN
, CLOSE
, PRINT
, INPUT
, and other related file-handling commands can significantly enhance your programming skills in QBASIC.