Subsections

Reading books

Before you can read the contents of an existing book, you need to connect the book to your program. The procedure open with the header

   PROC open = (REF FILE f,
                STRING idf,
                CHANNEL chan)INT:

performs that function. open yields zero if the connection is established and non-zero otherwise. Here is a program fragment which establishes communication with a read-only book whose identification is testdata:

   FILE inf;

   IF open(inf,"testdata",stand in channel)/=0
   THEN
      print(("Cannot open book testdata",
             newline));
      exit(1)
   FI

Notice that the program displays a short message on the screen if for any reason the book cannot be opened and then terminates with a suitable error number. The procedure exit is not standard Algol 68, but is provided by a system routine whose declaration is in the standard prelude issued with the a68toc compiler.

After a book has been opened, data can be read from the book using the procedure get which transforms external values into internal values like read (you will meet read again shortly). It has the header

   PROC get=(REF FILE f,[]SIMPLIN items)VOID:

The mode SIMPLIN is declared in the standard prelude as

   MODE SIMPLIN=
      UNION(
       REF CHAR, REF[]CHAR,  REF STRING,
       REF BOOL, REF[]BOOL,
   
       REF LONG BITS,        REF[]LONG BITS,
       REF BITS,             REF[]BITS,
       REF SHORT BITS,       REF[]SHORT BITS,
       REF SHORT SHORT BITS,
                        REF[]SHORT SHORT BITS,
   
       REF LONG INT,         REF[]LONG INT,
       REF INT,              REF[]INT,
       REF SHORT INT,        REF[]SHORT INT,
       REF SHORT SHORT INT,
                        REF[]SHORT SHORT INT,
   
       REF REAL,             REF[]REAL,
       REF SHORT REAL,       REF[]SHORT REAL,
   
       REF COMPL,            REF[]COMPL,
       REF SHORT COMPL,      REF[]SHORT COMPL,
            
       STRAIGHT SIMPLIN
      ),

The mode BITS is covered in chapter 11 together with LONG and SHORT modes. As you can see, all the constituent modes of the union are the modes of names, except for the STRAIGHT SIMPLIN and the PROC(REF FILE)VOID. The PROC mode lets you use routines like newpage and newline as one of the parameters. The actual header of newline is

   PROC newline = (REF FILE f)VOID:

and you can call it outwith get if you want. On input, the rest of the current line is skipped and a new line started. The position in the book is at the start of the new line, just before the first character of that line. Here is a program fragment which opens a book and then reads the first line and makes a name of mode REF STRING to refer to it. After reading the string, newline is called explicitly:

   FILE inf;
   open(inf,"book",stand in channel);
   STRING line;  get(inf,line);  newline(inf)

This could equally well have been written

   FILE inf;
   open(inf,"book",stand in channel);
   STRING line;  get(inf,(line,newline))

There is no reason why you should not declare your own procedures with mode PROC(REF FILE)VOID. Here is an example:

   PROC nl3 = (REF FILE f)VOID:
      TO 3 DO newline(f) OD;

This procedure could then be used in get, for example:

   STRING line1, line2;
   get(inf,(line1,nl3,line2))

where line2 would be separated by 2 skipped lines from line1.

The STRAIGHT operator converts any structure or multiple into a row of values of the constituent fields or elements. This means that get can read directly any structure or multiple (or even rows of structures or multiples).

There are four names of mode REF FILE declared in the standard prelude. One of these is identified by stand in. The procedure read which you have already used is declared as

   PROC read=([]SIMPLIN items)VOID:
      get(stand in,items)

in the standard prelude. As you can see, it gets data from stand in. If you want to, you can use get with stand in instead of read. The file stand in is already open when your program starts and should not be closed10.2. Note that input from stand in is unbuffered, that is, it does not use the channel stand in channel.

When you have finished reading data from a book, you should sever the connection between the book and your program by calling the procedure close. This closes the book. Its header is

   PROC close=(REF FILE f)VOID:

Exercises

9.1
Write a program called list which will read lines from a text book until a zero length line is read. The program should display each line on the screen on separate lines. Ans[*]
9.2
Write a program which will read a positive integer from a text book and which will then read that number of numbers (integer or real) from the book and display their total. Ans[*]


Sian Mountbatten 2012-01-19