• Keine Ergebnisse gefunden

Sequential Data Storage

Im Dokument THE ANATOMY OF THE 1541 DISK DRIVE (Seite 28-32)

A disk drive need not be used exclusively for storing pro-grams. If you have written a program that manages a large quantity of data, you need a fast way of organizing it.

Sequential data storage is not the fastest, but i t is the easiest method of managing data. This method is comparable to sequential storage on a cassette, which can be maintained in a program as such:

1. Load the program

2. Read the entire data file into the memory of the computer 3. Work with the data in memory (change, delete, combine) 4. Write the new file on an external medium (cassette,

diskette)

5. Exit the program

The maximum number of data items that the program can handle depends on the size of the computer's memory, because a single data item cannot be changed or erased directly on the cassette or diskette. To that end, the entire set of data items must be read in, changed, and then rewritten again.

Reading and rewriting the data occurs remarkably faster on a disk drive than on cassette.

It is worth mentioning that programs which work with sequential data on cassettes can be easily modified to work with disk. Only the corresponding OPEN commands need be changed.

1.4.1 The Principle

A sequential data file consists of several data records that are further divided into fields. The following is a name and address file and illustrates the principle of sequential data storage. Individual names and addresses comprise the data records of this file. A record consists of several fields (last name, first name, etc.). The structure of the file looks something like this:

Field 1 : Field 2 : Field 3 : Field 1 : Field 2 : Field 3 :

Data record 1 Data record 2

FILE

20

Only two records are shown above. The data records of a file are stored one after another (sequentially) as are the the fields within each record. The fields and records may be of any length. For example, field 1 of record 1 may be longer than field 1 of record 2. This is possible because the fields are separated from each other by a special character (the RETURN character), which is generated by the PRINT#

statement. When read back into the computer by the INPUT#

statement, the RETURN character is recognized as a field separator.

Each field is associated with a variable when written with a PRINT# statement or read with an INPUT# statement.

How does the computer know, when reading the data, where each field ends? Each field ends with a RETURN character.

The RETURN character has the decimal ASCII value 13. An example of a telephone directory file illustrates this. Our telephone directory file has three fields:

FIELD 1 FIELD 2 FIELD 3

LAST NAME FIRST NAME

TELEPHONE EXTENSION

Let's look at a section of this previously written file (the character + symbolizes a RETURN):

position:

Data:

1111111111222222222233333333334444444 1234567890123456789012345678901234567890123456 SMITH+JOHN+236+LONG+TIM+121+HARRIS+SAM+654+ •••

You can see that the fields are of different lengths and are all separated by a RETURN character. This RETURN character is automatically written after the data field by a PRINT#

statement, provided the PRINT# statement is not followed by a semicolon (which suppresses the RETURN character).

These data items are assigned to the variables with an INPUT# statement. After that, another INPUT# must follow in order to read the next field, and so on. The following sections explain the fundamentals of writing programs using sequential data storage.

1.4.2 Opening a Sequential Data File

To create a sequential data file, you must first OPEN the file. When opening a file to be written to, the following is carried out:

1. The diskette is checked to-see if an existing file has 21

Anatomy of the 1541 Disk Drive

the same name. If so, the error message FILE EXISTS is given by the DOS.

2. The file entry in the directory is written. In the file type it is noted that this file is not yet CLOSEd. This appears in a directory listing with an asterisk which preceeds the file type.

3. A free block is found, into which the first data items are written. The address (track and sector) of this free block is stored in the file entry of the directory.

4. The number of blocks in the file is set to 0, because no blocks of the file have been written yet.

The OPEN command specifies for what purpose (mode) the file is to be used (reading or writing). The format of the OPEN command looks like this:

OPEN

Ifn.8,sa,-filename.filetype.mode-When the logical file number is between 1 and 127, a PRINTi statement sends a RETURN character to the file after each variable. If the logical file number is greater than 127 (128-255), the PRINT* statement sends an additional line-feed after each RETURN. This is necessary for printers, for example, that do not provide an automatic line-feed after a RETURN character.

The secondary address (sa) can bE> a value between 2 and 14.

The secondary address indicates the channel over which the computer is to transfer data to and from the disk drive.

Secondary addresses 0 and 1 are reserved by the DOS for saving and loading programs. Secondary address 15 is desig-nated as the command and error channel. Should several files be open at once, they must all use different secondary addresses, as only one file can use a channel. If, however, a file is opened with the secondary address of a previously opened file, the previous file is closed.

A maximum of 3 channels can be opened with the VIC-1541 at a time. When utilizing relative data files, the DOS requires 2 channels per file. Therefore, the following maximum combinations are possible:

- 1 relative and 1 sequential file or - 3 sequential files

When specifying the filename to be written to (in the OPEN command), you must be sure that the fi Ie name does not already exist on the diskette. If a file that already exists is to be to opened for writing, an at sign followed by a colon (@:) must be placed in front of the file name (same as in the SAVE command). For example:

22

OPEN

1.8,2.-@:ADDRESSES.S.W-The file type must be given when the file is opened. 1.8,2.-@:ADDRESSES.S.W-The file type may be shortened to one of following:

S - sequential file U - user file P - program R - relative file

User files are sequential files that are listed in the directory with the file type USR. It is not a data file in the true sense. This file type is usually used when output that normally goes to the screen (BASIC listing, directory) is sent to the disk. In section 1.4.6 you find a description of this technique.

The last parameter (mode) establishes how the channel will used. There are four possibilities:

W - Write a file (WRITE - section 1.4.3) R - Read a file (READ - section 1.4.4) A - Add to a sequential file

(APPEND - section 1.4.4)

M - read a file that has not been closed ("discovered" by us in the DOS listing and

explained in section 1.4.5)

Now open a sequential file with the name SEQU.TEST for writing:

OPEN

1.8.2.-SEOU.TEST.S.W-If you now load the directory with LOAD-$-.8 and then LIST it, you see this file listed with an asterisk before the file type:

o

SEQU.TEST *SEO

But you are no longer allowed to close this file! After a file is OPENed and data written to it, it must be closed before the directory is loaded!

While a file is open, the command/error channel 15 may be opened, but when channel 15 is closed, all other channels are closed as well. You must take note of this.

Now some examples of the OPEN command:

OPEN 1,8,2,"SEOU.TEST,S,R"

OPEN 2,8,3,"SEOU.TEST,l],W"

OPEN 3,8,4,"TEST,P,R"

23

- open a sequential file for reading

- open a user file for writing - open a program file for

reading

AnatOItty of the 1541 Disk Drive

OPEN 4,8,5,"SEOU.TEST,S,A" - open a sequential file for appending data

OPEN 5,8,6,"CSTMRS.1983,S,M" - open the unclosed customer file for reading

1.4.3 Transferring Data Between Disk and Computer

After opening a file for writing, you transfer data to be stored to the diskette with the PRINTiF statement. This statement transmits an additional RETURN that is required for separating data. In the following example, a file is OPENed, data written to it, and CLOSEd again. PRINT# cen also be used as a direct command, that is, outside of the program, so the following commands can be typed one after the other and executed. Now open a file with the name

"TEST":

OPEN

1,8,2,-TEST,S,W-You should notice that the red LED on the disk drive was lit. It signals the fact that a file was OPENed. You can now write to the file named TEST. Here is how we would write a name and address record consisting of 4 fields:

PRINT# 1, "SAM"

PRINT#I,"HARRIS"

PRINT#l,"2001 MAIN STREET"

PRINT# 1, "ANYTOWN"

Now these data items have been written to the file so we can close the file with CLOSE 1. The red LED should go out. In order to read this data again, you must open the file in the read mode (R). Because the INPUT# statement cannot be used directly, a small program must be written:

10 OPEN 1,8,2,"TEST,S,R"

20 INPUT#I,FNS

Im Dokument THE ANATOMY OF THE 1541 DISK DRIVE (Seite 28-32)