Keywords: pame

Subsections

- Exercises

# Writing to books

You should use the `establish` procedure to create a new book. Here is a program fragment which creates a new book called `results`:

       FILE outf;

       IF establish(outf,
                    "results",
                    stand out channel,
                    0,0,0)/=0
       THEN
          print(("Cannot establish book results",
                 newline));
          exit(1)
       FI

As you can see, `establish` has a similar header to `open`. What are the integers used for? The header for `establish` is

       PROC establish = (REF FILE f,
                         STRING idf,
                         CHANNEL chann,
                         INT p,l,c)INT:

The `p`, `l` and `c` in `establish` determine the maximum number of pages, lines and characters in the book which is being created. Values of `0` for all three integers mean that the file should be established with zero length. However, they are ignored by the `stand out channel` in the QAD standard prelude provided with the a68toc compiler.

The procedure used to write data to a book is `put`. Its header is

       PROC put=(REF FILE f,[]SIMPLOUT items)VOID:

You can examine the source of the standard prelude to see how the mode `SIMPLOUT` is declared.

Again, `newline` and `newpage` can be used independently of `put` as in the following fragment:

       FILE outf;
       IF establish(outf,
                    "newbook",
                    stand out channel,
                    0,0,0)/=0
       THEN
          put(stand error,
              ("Cannot establish newbook",
               newline));
          exit(2)
       ELSE
          put(outf,("Data for newbook",newline));
          FOR i TO 1000 DO put(outf,i) OD;
          newline(outf);
          close(outf)
       FI

On output, the newline character is written to the book.

`newpage` behaves just like `newline` except that a form feed character is searched for on input, and written on output.

The procedure `establish` can fail if the disk you are writing to is full or you do not have write access (in a network, for example) in which case it will return a non-zero value.

When you have completed sending data to a book, you must close it with the `close` procedure. This is particularly important with books you write to because the channel is buffered as explained above. Using `close` ensures that any remaining data in the buffer is flushed to the book.

The procedure `print` uses the `REF FILE` name `stand out`. So

       print(("Your name",newline))

is equivalent to

       put(stand out,("Your name",newline))

Again, `stand out` is open when your program is started and it should not be closed. Transput via `stand out` is unbuffered. You cannot read from `stand out`, nor write to `stand in`. The procedure `write` is synonymous with `print`.

## Exercises

9.3
  Change the second program in the last set of exercises to put its total into a newly-created book whose identification is `result`. Ans
9.4
  Adapt Eratosthenes' Sieve (see section 5.4.1) to output all the prime numbers less than 10,000 into a book called `primes`. Ans