Subsections

Binary books

In section 9.1, it was mentioned that some books contain data in a compact form which is not usually human-readable. Most large books, especially those containing design figures in the engineering sciences as well as books containing the payroll data for a number of employees, will be stored in this form. They are called binary books.

Algol 68 allows you to write anything to binary books, just as for text books. Indeed, you can write an integer and a character to a binary book and then read back the data as a character followed by an integer. The results may not be particularly meaningful, but you can do it.

The only difference between transput to, or from, binary books is that instead of using the procedures put and get, you use the procedures put bin and get bin. The modes accepted by these procedures are identical with those accepted by put and get respectively except that you cannot transput procedures with mode

   PROC(REF FILE)BOOL

Here is a program which will output the data produced by the program in the last exercise:

   PROGRAM binary CONTEXT VOID
   USE standard
   BEGIN
     FILE f;

     IF establish(f,
                  "pipowers",
                  stand out channel)/=0
     THEN
       put(stand error,
           ("Cannot create pipowers",
            newline));
       stop
     FI;

     FOR i TO 10
     DO
       put bin(f,(i,pi**i))
     OD;
     close(f)
   END
   FINISH

Run the program and then look at the book it has produced. Compare it with the data produced by the program in the last exercise.

Values of mode REF STRING can be read by get bin, but you should remember to set the string terminator using the procedure make term. However, you should note that the string terminator will always include the character lf. Furthermore, if set possible is FALSE for the REF FILE on which transput is being performed, the terminator will have been read when the routine get bin returns. If set possible is TRUE for that REF FILE, then the terminator will not have been read.

Another aspect of binary books is that of being able to browse. The principal procedure provided for this purpose is set which has the header

   PROC set=(REF FILE f,INT p,l,c)VOID:

The last three parameters specify the position in the book where you want to start browsing, whether reading or writing. The QAD transput provided with the a68toc compiler ignores the p and l parameters because it regards a file as consisting of one page of one line.10.5

There are two other related procedures. One is reset which has the header

   PROC reset=(REF FILE f)VOID:

and is equivalent to set(f,0,0,0). One possible use of this procedure is to output data to a book, then use reset followed by get to read the data from the book. The sort of book used in this way is often called a work file (in operating system terms). Such a book contains data of use while a program is being elaborated, but is deleted at the end of the program. In fact, every program has such a book whose controlling FILE is called stand back. It uses the stand back channel and is deleted when the program has finished. However, you can write to it, reset it, then read the contents and copy them to another book. Note that the procedure read bin is equivalent to get bin(stand back,...) and the procedure write bin is equivalent to put bin(standback,...).

The other related procedure is logical end which has the header

   PROC logical end = (REF FILE f)INT:

and yields the value of the position at the end of the book, which is the size of the book. The position can be set to the end of the book by writing

   set(f,0,0,logical end(f))

Here is a procedure which opens an existing book and sets the writing position to its end, then writes data to the end of the book:

   PROC debug=(REF FILE dbg,[]SIMPLOUT st)VOID:
   (
     open(dbg,idf(dbg),stand back channel);
     set(dbg,0,0,logical end(dbg));
     put(dbg,st);
     close(dbg)
   )

We shall use this procedure in chapter 12 to store data about the running of a program while we are developing it. Notice that textual data is written to the book even though the procedures set and logical end are used. The point is that binary and textual data can be mixed in any book which allows binary transput.

In the QAD standard prelude, the current position in a book can be obtained from the procedure current pos which has the header

   PROC current pos = (REF FILE f)POS:

This particular procedure is very useful if you want to store the book position of the beginning of a group of data in a book (such a group is often called a record). In the QAD standard prelude, POS is a synonym for INT.


Exercises

9.16
Write a program which creates a binary book containing the first 1000 whole numbers. Use set to read every 17th number and display them on the screen, one to a line. Ans[*]
9.17
Write a program to read a book containing text and write each individual word to one book, and the position of the start of each word and the length of the word to another book. Both output books should be written using put bin. Ans[*]


Sian Mountbatten 2012-01-19