Classic Computer Magazine Archive ANTIC VOL. 5, NO. 1 / MAY 1986

ST BASIC DISK I/O

Random access file control

by DAVID STAMBAUGH

The Atari 520ST computer breaks new ground for personal computer owners in the area of power per buck and graphics capabilities. But for those who bought an ST expecting to transfer their 8-bit Atari BASIC programming skills unchanged--hefty surprises are in store. One of the biggest changes is in the area of random disk file handling. And in this article I will give you a sample of how the ST handles random files.

RANDOM ACCESS

Basically, there are two different types of files--sequential and random. Sequential files are like a spool of recording tape. If you want to see what's at the end, you need to unwind the whole tape. That takes time.

However, random access files are like LP Records. To see what's at the end, just skip over everything else and start reading (or writing) wherever you need. This saves time because now you don't need to read 349 items to get to the 350th, you just move right to the350th item and start reading.

PROGRAM BREAK-DOWN

Examine Listing 1. This ST BASIC program demonstrates how a random access file is created, written to and read from.

First, you establish some constants in lines 150-160, then clear and erase the output window. The OPEN command in line 190 is somewhat like the OPEN in 8-bit Atari BASIC. You assign the file number, the access type and the filename, but here you also tell it the length of the record. This is because ST random access files use fixed-length records.

OPEN SESAME

The command structure is: OPEN [mode], [file number], [filename], [record length]. The mode can be O for sequential file output, I for sequential input, or R for random file access. The file number can be any number between 1 and 15, preceeded by the # sign. And, as far as I have been able to figure out, there are no pre-assigned codes such as the 6 was for 8-bit BASIC.

The filename is enclosed in double quotes and consists of a drive specifier (A: or B:), a filename with the familiar up-to-eight-letter-name period and up-to-three-letter-extender. At the end of all this is the record length. This is optional and defaults to 128 bytes, but should be set at the length of your individual record. Random access files require that all of your records occupy the same amount of space regardless of the actual length.

RANDOM FIELDS FOREVER

The FIELD command, in line 200, sets aside space to be used as a buffer for the random file access. You don't directly move data from a string to the disk. Instead you move it to the buffer (described shortly) and then use the PUT statement to write the record to the disk, or the GET statement to read the entire record.

The format of this statement is FIELD [file number], [field width] AS [string variable], etc. For example: FIELD #1, 10 AS PHONE$, 25 AS PERSONAL$. This is a bit strange-looking if you are used to Atari 8-bit BASIC, so let's examine it more closely.

The file number is just like the OPEN command and can be from 1 to 15. Using the above example, the field width instructs the computer to use the first 10 characters as PHONE$, and the next 25 as PERSONAL$. One important thing to keep in mind is that the sum total of the field widths in the FIELD command should be exactly the same as the length specified in the OPEN command.

RSET & LSET

The RSET and LSET commands (line 290) move the data from the string variables you are using (A$ and B$ in the example program) to the buffer area for the random files (PHONE$ and PERSONAL$ in the example program). Do not try to re-assign the buffer string set aside to a variable value using a LET statement (LET PERSONAL$ =A$ or PERSONAL$=A$). Doing this will simply move the variable pointer away from the buffer area and defeat what you're trying to do. RSET will right-justify the data as needed and either truncate or pad with blanks if needed. LSET does the same except that it switches to left-justification.

USING GET

The GET command (line 550) has the format of GET [file number], [record number], with the record number being an integer variable within the range of 1 to 32767. This command will read the next [record length from the OPEN command] number of characters from the file accessed through file [file number].

The data is placed into the random access data buffer as outlined in the FIELD command. Your program then needs to move the data to the variables involved, using either the LSET or RSET command. It is possible to try and access data beyound the range of the actual file scope, so your program needs to somehow handle this potential problem.

PURSUING PUT

The PUT command (line 310) has the format of PUT [file number], [record number] with the record number being an integer variable within the range 1 to 32767. This command will take the data in the random access buffer defined by the FIELD command and write it to disk in the [record number] position within the file.

When writing to the file for the frist time, you must write the file in sequential order. Note that you must use the LSET or RSET command to move the data to the random access buffer bcfore issuing the PUT command.

CLOSING IT UP

The CLOSE command (line 460) takes the form CLOSE #[file number],[file number], etc. This will close the specified open file(s), flushing the data buffers to the disk if necessary. The file number is optional and issuing the CLOSE command without a file number will close all open files.

RANDOMLY ENDING

This brief introduction does not even begin to explain how to use numeric variables with random access files. (HINT: Look up the MKD$, MKL$ and the MKS$ commands.) Experiment with this feature, and see how fast you can access data.

Dave Stambaugh programs DEC PDP-II computers for the Caterpillar Tractor Company in East Peoria, IL. Since 1982, he has owned every Atari computer model except the 600XL. He is a past president of the 400-member Peoria Atari Computer Enthusiasts.