Introduction to File Handling in QBasic
File handling in QBasic is an essential skill that allows programmers to read from and write to files, enabling data persistence across different sessions. Whether you are storing user data, configuration settings, or logs, mastering file handling opens up a range of possibilities. In this article, we’ll explore the various file handling functionalities available in QBasic, including opening, reading, writing, and closing text files.
Types of Files in QBasic
In QBasic, you primarily deal with two types of files:
-
Text Files: These files store data in plain text format, making them easily human-readable. You can use text files for storing lists of items, descriptions, or even full documents.
-
Random Access Files: These files allow you to access data at any point within the file without having to read through the entire file sequentially. This is a more advanced topic, but it’s worth noting for later exploration.
For this tutorial, we’ll focus primarily on text file handling, as it is more straightforward and commonly used.
Opening Files
Before you can work with a file, you need to open it. In QBasic, you use the OPEN statement to open a file. The syntax for opening a file is as follows:
OPEN "filename" FOR mode AS #fileNumber
- filename: The name of the file you want to open.
- mode: The mode in which you want to open the file, either for input, output, or random access.
- fileNumber: A number assigned to the file for use in subsequent file operations.
Modes for Opening Files
QBasic allows you to open files in several modes:
- Input:
FOR INPUTis used when you want to read data from a file. - Output:
FOR OUTPUTis used to create a new file or overwrite an existing file with new data. - Append:
FOR APPENDallows you to add data to the end of a file without overwriting the existing content.
OPEN "data.txt" FOR INPUT AS #1 ' Open a text file for reading
OPEN "output.txt" FOR OUTPUT AS #2 ' Open a text file for writing
OPEN "log.txt" FOR APPEND AS #3 ' Open a text file for appending
Reading from Files
Once you have opened a file in input mode, you can read data from it. The most common ways to read from a text file in QBasic are using the INPUT statement or the LINE INPUT statement.
Using the INPUT Statement
The INPUT statement reads data from a file and assigns it to variables. Here’s an example:
DIM name AS STRING
DIM age AS INTEGER
OPEN "data.txt" FOR INPUT AS #1
INPUT #1, name, age ' read name and age from the file
CLOSE #1 ' close the file after reading
PRINT "Name: "; name
PRINT "Age: "; age
Using the LINE INPUT Statement
If you want to read an entire line of text from the file, you can use the LINE INPUT statement. This is useful for reading data that contains spaces.
DIM line AS STRING
OPEN "data.txt" FOR INPUT AS #1
LINE INPUT #1, line ' read a full line from the file
CLOSE #1 ' close the file
PRINT "Line: "; line
Writing to Files
Writing data to a file can be done using the PRINT or WRITE statement. When using FOR OUTPUT, you can either create a new file or overwrite an existing file. If you are using FOR APPEND, the data will be appended to the end of the file.
Using the PRINT Statement
The PRINT statement writes text to a file. Here's an example:
DIM name AS STRING
DIM age AS INTEGER
name = "John Doe"
age = 30
OPEN "output.txt" FOR OUTPUT AS #1
PRINT #1, name ' write name to the file
PRINT #1, age ' write age to the file
CLOSE #1 ' close the file after writing
Using the WRITE Statement
The WRITE statement is similar to PRINT, but it formats the output in a way that can be read back accurately. It automatically adds quotes around strings and uses commas as delimiters.
OPEN "data.csv" FOR OUTPUT AS #1
WRITE #1, name, age ' write name and age with formatting
CLOSE #1 ' close the file after writing
Error Handling
When working with files, it’s essential to handle potential errors gracefully. You can use the ON ERROR statement to handle runtime errors. For example:
ON ERROR GOTO ErrorHandler
OPEN "data.txt" FOR INPUT AS #1
' (Perform file operations...)
CLOSE #1
EXIT SUB
ErrorHandler:
PRINT "An error occurred while accessing the file."
Closing Files
After you are done with file operations, it is essential to close the file using the CLOSE statement. This releases the resources associated with the file and saves any changes if applicable.
CLOSE #1 ' close the file
Complete Example
Now that we have covered the basics, let’s put everything together in a simple QBasic program. This program will create a file, write some data to it, read the data back, and display it on the screen.
' Create and write to a text file
DIM name AS STRING
DIM age AS INTEGER
name = "Alice"
age = 25
OPEN "userData.txt" FOR OUTPUT AS #1
PRINT #1, name
PRINT #1, age
CLOSE #1
' Read from the text file
DIM readName AS STRING
DIM readAge AS INTEGER
OPEN "userData.txt" FOR INPUT AS #1
INPUT #1, readName, readAge
CLOSE #1
' Display the read data
PRINT "Name: "; readName
PRINT "Age: "; readAge
Conclusion
File handling in QBasic is a powerful feature that enhances your programming capabilities. By learning how to open, read, write, and close files, you can create applications that persist data, making them more robust and user-friendly. As you continue your programming journey, mastering file handling will enable you to tackle more complex tasks and projects.
Happy coding!