next up previous contents index
Next: Caveats Up: Input and Output Previous: Control Strings

Customized I/O Routines

 

There are two restrictions on reading and writing parallel variables that can be corrected by using customized I/O routines. First, parallel variables are by default read or written one element at a time; customized I/O routines provide a way to print two parallel variables side by side. Second, customized I/O routines provide a way to print derived data types as a single expression.

For example, if People is a parallel array of records, the following statement will first write all of the name fields followed by all of the age fields.

    [R] write(People.name, People.age);
  A customized I/O routine is a procedure that specifies how to print a single element of a parallel variable. Once bound to a parallel variable using bind_write_func(), subsequent I/O calls will use the user-supplied procedure to print each element of the parallel variable. Thus, the following will cause the name and age fields to be printed side by side:

    procedure Write_Person(outfile: file; var p: person);
    begin
        write(outfile, p.name, p.age);
    end;

    . . .
    bind_write_func(People, Write_Person);
    [R] writeln(People);
The first parameter to bind_write_func() is a file parameter. The second parameter is one element of the parallel variable. The second parameter must be a var parameter but must not be modified inside the procedure body.

  The customized print routine can be unbound using unbind_write_func() with the parallel variable's name as a parameter:

    unbind_write_func(People);
    A pair of analogous routines, bind_read_func() and unbind_read_func(), are supplied for reading parallel variables.



Kay Nettle
Fri Feb 21 21:14:29 CST 1997