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

Read and Write

     

ZPL's read and write statements are used to transfer basic, non-record or programmer-defined type, variables to and from files. The read and write statements have the following syntax, where file is an optional file parameter and exprls is an arbitrary list of expressions.

 
		 read    		([file,]   		exprls);

write ([file,] exprls);

writeln ([file,] exprls);

The file parameter specifies the file to access. If this parameter is not specified, input comes from stdin and output goes to stdout (stdin and stdout are always open and never need to be closed). For read(), the list of expressions is a list of variables to read into. For write(), the list of expressions is a list of variables to write. These expressions may be either sequential or parallel variables. If any are parallel variables a region must apply to the statement to specify the portions of the parallel variable to read or write. The writeln() procedure is identical to write() except it writes a newline character after writing its expression list.

    var f : file;
        i : integer;
        A : [R] float;
    . . .
    read(f, i);               -- read an integer into variable i
[R] read(f, A);               -- read a parallel of floats into variable A
    close (f);                -- close the file
The read(); and write(); statements take a variable number of expressions, so the above two read(); statements could have been written as one:

[R] read(f, i, A);            -- equivalent to the two reads() above

Parallel variables are serialized when they are written to a file. Thus, the file contains a sequence of elements and all region structure is lost. This means that if a parallel variable is written to a file using one region and is then read from the file using a subset of that region, the values that are read will not correspond to those in the subregion of the original parallel variable. Similarly, care must be taken when applying masked regions to I/O statements (Section 5.3.5 describes masks).



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