8 File Storage

Serial Access

Serial access is used to store or retrieve data items one after the other, without regard to logical record bounds. For each data file opened, the data pointers keep track of the data item currently being accessed. As you store or retrieve data, the pointers move serially forward through the data file.

The Serial PRINT# Statement

The serial PRINT# statement records values onto the specified file from the specified variables or strings. Syntax for this statement is as follows:

The data list is a collection of items separated by commas. The items can be variables, array identifiers, and numeric or string expressions. Including the optional END causes an EOF mark to be printed at the end of the data; otherwise, an EOR mark is placed after the data list is printed.

Printing begins at the position of the data pointers (which is after the data item most recently stored or retrieved) or at the beginning of the file if nothing has been stored or retrieved. The record pointer can also be repositioned to the beginning of the file (see page 231 ).

Here is a simple program which creates a file named CLASS and prints the names and grades of five students:

30   FOR I=1 TO 5
50     PRINT #1;N$,S
60   NEXT I
70   PRINT #1;END
80   END
Line 50 prints students' names and grades, alternately, in the file. Line 70 places an EOF mark after the five sets of data are printed. The EOF prevents reading data beyond its position.

When printing a long string, it might possibly be too long to be contained in one logical record. In this case, the string is automatically broken up and stored into a series of logical records. This requires an additional two words each time the string crosses over into another logical record. The parts of the string are identified at the first record, intermediate records, and the last record.

Data can be stored using the PRINT# statement in a file created with the SAVE statement. SAVE, in effect, performs a serial print into a file.

Here are two examples:

100   PRINT #3;Apples,Bananas,Carrots
110   PRINT #3;Donuts,Eggs(*)
These two statements store values for all five variables into file #3. The EOR which was placed after the data when line 100 was executed is overwritten when line 110 is executed. Another EOR is printed after the data in line 110. Remember, an EOR signifies that there is no more data between the data pointers and the end of the record.

The serial PRINT# statement can also be used to generate program lines into a file. Such a file can be retrieved with GET.

Here are two examples:

50   P$="COUNTR"
60   CREATE P$,3,50
70   ASSIGN #1 TO P$
80   PRINT #1;"10 FOR I=1 TO 10","20 PRINT I","30 NEXT I","40 END"
90   GET P$,10,10

Executing LIST produces:

10 FOR I=1 TO 10
40 END
Below you find two examples concerning printing of User Defined Type variables.

In the example below, the Comment$ member variable from the Phone1 variable is PRINTed.

Phone1.Comment$="*Fancy Comment*"
PRINT #3;Phone1.Comment$
In addition to accessing single variables, you can specify the whole variable at once.

The example below prints all member variables of Phone1:


The Serial READ# Statement

The serial READ# statement retrieves values for variables and strings of characters from the specified file. Syntax for this statement is as follows:

READ# file number; variable list

Before you can use data which has been stored in a data file with a PRINT# statement, you must read the data back into the computer memory. The data is not erased from the file, it is merely copied into the variables specified in the same order in which it was stored with the PRINT# statement. The User Defined Types can be used in the same way as with PRINT#. Variables do not have to have the same names specified in the PRINT# statement. Reading begins after the last item printed or read on the specified file. To begin reading from the beginning of the file, you must reposition the record pointer (see page 231 ) or do another ASSIGN.

As an example, the data printed in the previous example in a file named CLASS can be read by using this program:

20   PRINT "    NAME           GRADE"
30   FOR I=1 TO 5
40     READ #1;Name$,Score
50     PRINT Name$,Score
60   NEXT I
70   END
Notice that the serial READ# statement must specify the types of data (data elements or string variable) in the order in which they were originally stored in the file. Line 40 reads a string variable and then a numeric variable. This program can run only when the order of the data on file is known. Here is the printout:

    NAME           GRADE

Charlie Brown       79
Casey Jones         99
Sean Jackson        91
Jack Allison        83
Sam Amigo           95
The variables into which you read data items need not have the same names used when the items were printed on the file. Although the variable name changes (from N$ and S when stored, to Name$ and Score when retrieved), the order in which the two data types are accessed is the same.

When a serial READ# statement encounters the EOF mark previously placed by the last PRINT# statement, the program ends and an error indicates the end of the file. The program can be written to run without displaying an error by using the ON END# statement, described later in this chapter.

Positioning the Record Pointer

It is often necessary to position the record pointer to the beginning of a specific record in a file before executing a serial READ# statement. This is done by using only file number and record number parameters in a direct READ# statement:

READ# file number ,record number

A serial PRINT# or READ# statement can then be executed to access the beginning of the specified record, rather than the beginning of only the first record in the file.

To see how this works, first use the next program to store consecutive values beginning from the 8th record of a file named NUMBERS:

30   READ #1,8
40   FOR Value=1 TO 300
50     PRINT #1;Value
60   NEXT Value
70   END
The ASSIGN statement sets the record pointer to the beginning of the first record in the file. The pointer is then repositioned to the beginning of the eighth record by the READ# statement. The FORNEXT and PRINT statements fill the file with the numbers 1 through 300, starting at the eighth record.

Now use the following program to read and display the data, beginning at record 14:

10   DIM A(7)
30   READ #1,14
40   FOR I=1 TO 12
50     READ #1;A(*)
60     DISP A(*)
70   NEXT I
80   END

  193   194   195   196   197   198   199   200     record 14
  201   202   203   204   205   206   207   208
  209   210   211   212   213   214   215   216
  217   218   219   220   221   222   223   224

  225   226   227   228   229   230   231   232     record 15
  233   234   235   236   237   238   239   240
  241   242   243   244   245   246   247   248
  249   250   251   252   253   254   255   256

  257   258   259   260   261   262   263   264     record 16
  265   266   267   268   269   270   271   272
  273   274   275   276   277   278   279   280
  281   282   283   284   285   286   287   288
The ASSIGN statement automatically sets the record pointer to the beginning of the first record. The pointer is then repositioned to the beginning of record 14 by line 30. The serial READ# statement begins reading data from that point on.

NOTE: Reading record 17 causes an error if more than 12 values are read. Record 17 contains only 12 values (289-300).

Since each real-precision value uses 8 bytes of memory, 32 values can be printed into a 256-byte record. On the file NUMBERS, for example, the following values are stored on these corresponding records:

Comparison of Data Access Methods
Record No. Full-precision Values
1 through 7(none)
81 through 32
933 through 64
1065 through 96
1197 through 128
12129 through 160
13161 through 192
14193 through 224
15225 through 256
16257 through 288
17289 through 300

Data read must correspond to the type (numeric or string) that was printed. However, a numeric data item need not be one of the same precision. Precision is automatically converted. You can also print an array and read back simple variables or other arrays and vice versa.

Eloquence Language Manual - 19 DEC 2002