Using the QuickBASIC Open Command

Z. Perry
The "Open" command in the QuickBASIC programming language can be used to open files so that they can be read from, written to, or both. This command is often used in conjunction with the "print", "input", and "close" functions.

Although there is other syntax which can be applied, the Open command is often used in this general format:

OPEN FOR AS #

Here is an example of this, which would open "example.dat" from the floppy drive so that it can be written to:

OPEN "A:\EXAMPLE.DAT" FOR OUTPUT AS #1

The part of the Open command identifies the drive letter, directory/path (if any), name, and extension. The filename should not be any longer than eight characters in QuickBASIC, with an extension of 0-3 letters. The drive letter and directory (a.k.a. folder) are not necessary if it is located in the current directory or the same directory as the QuickBASIC program.

The can be output (write to file), input (read), append (add to the end), random (read/write random-access), or binary (read/write binary). When remembering which mode to use, it is helpful to think that the mode is from the perspective of the QuickBASIC program; it outputs (writes) to the file, and inputs from the it, as the mode names suggest. According to QuickBASIC 4.5 Help, the file number used for this command can be as low as one or as high as two-hundred and fifty-five.

In the following example, the above-mentioned filename is opened and the words "Example Data" are saved to it. Then it is closed, which should always be done when a program is finished working with a file:

OPEN "A:\EXAMPLE.DAT" FOR OUTPUT AS #1
PRINT #1, "Example Data"
CLOSE #1

Next, it is re-opened and the phrase "...Additional Data" is appended to it. Using the file number #2 works just as well as #1:

OPEN "A:\EXAMPLE.DAT" FOR APPEND AS #2
PRINT #2, "...Additional Data"
CLOSE #2

It goes on to open it again so as to load data (INPUT) from it, and adds the data to variable A$ until it reaches the End Of the File (EOF). Finally, it is closed and both phrases are displayed on-screen:

OPEN "A:\EXAMPLE.DAT" FOR INPUT AS #1
DO UNTIL EOF(1)
INPUT #1, B$
A$ = A$ + B$
LOOP
CLOSE #1
PRINT "Contents of EXAMPLE.DAT: "; A$

The "Input" mode will produce a "File Not Found" error if the file it tries to read from doesn't exist. On the other hand, "Output" and other modes will create a new file if it can't be found.

The "Random" mode should not be applied to standard text files, and uses "Get" and "Put" commands to save or load data from specific positions within the file. You can read more about using random access mode in QuickBASIC by clicking here and scrolling down near the end of the page. Only using the "Input", "Output", and "Append" modes is easier for beginners and are all that is necessary for this command in many programs.

Published by Z. Perry

Freelance writer, website operator, and programmer  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.