MAINSAIL Language Manual, Section 46.7

previous   next   top   contents   index   framed top   this page unframed


46.7. read

Figure 46–9. read (GENERIC)
PROCEDURE   read        (MODIFIES STRING s;
                         
PRODUCES REPEATABLE BOOLEAN v);

PROCEDURE   read        (POINTER(textFilef;
                         
PRODUCES REPEATABLE BOOLEAN v);

PROCEDURE   read        (POINTER(dataFilef;
                         
PRODUCES REPEATABLE BOOLEAN v);

$BUILTIN
PROCEDURE   read        (MODIFIES ADDRESS a;
                         
PRODUCES REPEATABLE BITS v);

PROCEDURE   read        (MODIFIES STRING s;
                         
PRODUCES REPEATABLE BITS v);

PROCEDURE   read        (POINTER(dataFilef;
                         
PRODUCES REPEATABLE BITS v);

PROCEDURE   read        (POINTER(textFilef;
                         
PRODUCES REPEATABLE BITS v);

PROCEDURE   read        (POINTER(dataFilef;
                         
PRODUCES REPEATABLE INTEGER v);

PROCEDURE   read        (POINTER(textFilef;
                         
PRODUCES REPEATABLE INTEGER v);

PROCEDURE   read        (MODIFIES STRING s;
                         
PRODUCES REPEATABLE INTEGER v);

$BUILTIN
PROCEDURE   read        (MODIFIES ADDRESS a;
                         
PRODUCES REPEATABLE INTEGER v);

PROCEDURE   read        (POINTER(dataFilef;
                         
PRODUCES REPEATABLE LONG BITS v);

PROCEDURE   read        (POINTER(textFilef;
                         
PRODUCES REPEATABLE LONG BITS v);

PROCEDURE   read        (MODIFIES STRING s;
                         
PRODUCES REPEATABLE LONG BITS v);

$BUILTIN
PROCEDURE   read        (MODIFIES ADDRESS a;
                         
PRODUCES REPEATABLE LONG BITS v);

PROCEDURE   read        (POINTER(dataFilef;
                         
PRODUCES REPEATABLE LONG INTEGER
                             
v);

PROCEDURE   read        (POINTER(textFilef;
                         
PRODUCES REPEATABLE LONG INTEGER
                             
v);

PROCEDURE   read        (MODIFIES STRING s;
                         
PRODUCES REPEATABLE LONG INTEGER
                             
v);

$BUILTIN
PROCEDURE   read        (MODIFIES ADDRESS a;
                         
PRODUCES REPEATABLE LONG INTEGER
                             
v);

PROCEDURE   read        (POINTER(dataFilef;
                         
PRODUCES REPEATABLE LONG REAL v);

PROCEDURE   read        (POINTER(textFilef;
                         
PRODUCES REPEATABLE LONG REAL v);

PROCEDURE   read        (MODIFIES STRING s;
                         
PRODUCES REPEATABLE LONG REAL v);

$BUILTIN
PROCEDURE   read        (MODIFIES ADDRESS a;
                         
PRODUCES REPEATABLE LONG REAL v);

PROCEDURE   read        (POINTER(dataFilef;
                         
PRODUCES REPEATABLE REAL v);

PROCEDURE   read        (POINTER(textFilef;
                         
PRODUCES REPEATABLE REAL v);

PROCEDURE   read        (MODIFIES STRING s;
                         
PRODUCES REPEATABLE REAL v);

$BUILTIN
PROCEDURE   read        (MODIFIES ADDRESS a;
                         
PRODUCES REPEATABLE REAL v);

$BUILTIN
PROCEDURE   read        (MODIFIES ADDRESS a;
                         
PRODUCES REPEATABLE STRING v);

PROCEDURE   read        (POINTER(textFilef;
                         
PRODUCES REPEATABLE STRING s);

PROCEDURE   read        (MODIFIES STRING s;
                         
PRODUCES REPEATABLE STRING v);

$BUILTIN
PROCEDURE   read        (MODIFIES ADDRESS a;
                         
PRODUCES REPEATABLE POINTER v);

$BUILTIN
PROCEDURE   read        (MODIFIES ADDRESS a;
                         
PRODUCES REPEATABLE ADDRESS v);

$BUILTIN
PROCEDURE   read        (MODIFIES ADDRESS a;
                         
PRODUCES REPEATABLE CHARADR v);

$BUILTIN
PROCEDURE   read        (MODIFIES ADDRESS a;
                         
PRODUCES REPEATABLE BOOLEAN v);

PROCEDURE   read        (POINTER(dataFilef;
                         
PRODUCES REPEATABLE STRING s);

PROCEDURE   read        (POINTER(textFilef;
                         
POINTER($areaarea;
                         
PRODUCES REPEATABLE STRING s);

PROCEDURE   read        (POINTER(dataFilef;
                         
POINTER($areaarea;
                         
PRODUCES REPEATABLE STRING s);

$BUILTIN
PROCEDURE   read        (MODIFIES ADDRESS a;
                         
PRODUCES REPEATABLE $PROCVAR v);

read reads values from an input file, a STRING, or a memory location.

The forms that read a BOOLEAN, (LONG) INTEGER, (LONG) REAL, or (LONG) BITS from a text file or STRING use a scan for the proper constant representation. As soon as the scan finds what could be the start of a constant of the desired type, it begins forming the value. All characters involved in the scan are removed from the source STRING or text file. Characters not involved in the scan are not removed; e.g., if the characters involved in the scan occur immediately before an eol, the eol is left in the source.

The forms of read that read a BOOLEAN from a text file or STRING scan for the STRING representation "TRUE" or "FALSE". Case is ignored. As soon as one of these STRING representations is found, or there are no more characters in the source STRING or text file, the scan stops. All scanned characters are removed from the source STRING or text file. The BOOLEAN value produced is TRUE if the characters TRUE were found in the source; otherwise, it is FALSE. The characters TRUE or FALSE need not be preceded or followed by a blank, tab, or end of line.

Numeric ((LONG) INTEGER and (LONG) REAL) and (LONG) BITS scans ignore dots, single quotes, and minuses that are not associated with valid digits. For example, if the STRING:

"These ' and - and . are ignoredthe value read is 2"

is scanned for a numeric or BITS, the 2 is found, and the symbols ', -, and . are ignored since they are not associated with valid digits.

The L that follows LONG constants as written in a source program is not used by the scan. For example, if the STRING:

"123L 456"

is scanned for a (LONG) INTEGER, 123 is returned, and the STRING becomes:

"L 456"

A (LONG) INTEGER scan looks for one or more digits, possibly preceded by -. For example, if the STRING:

"123. 456"

is scanned for an INTEGER, 123 is returned, and the STRING becomes:

". 456"

On the other hand, if it were scanned for a REAL, then 123. would be returned, and the STRING would become:

" 456"

A (LONG) REAL scan accepts an INTEGER constant. For example, if the STRING:

"123 456."

is scanned for a REAL, 123. is returned, and the STRING becomes:

" 456."

A BITS scan accepts the standard representation, i.e., ' optionally followed by a base letter, then digits. It also accepts a sequence of octal digits not preceded by '. For example, if the STRING:

"1238 '456"

is scanned for a BITS, '123 is returned, and the STRING becomes:

"8 '456"

The effect is undefined of reading a (LONG) INTEGER or (LONG) REAL from a STRING or text file if the text scanned represents a value outside the MAINSAIL guaranteed range.

A STRING read from a STRING or file returns the next line by scanning for eol (end-of-line), which is then discarded. Characters in a data file are stored one per character unit, as in a text file. If the file is opened for PDF I/O, characters may be translated to the host character set. In the area forms, area specifies the destination area for the resulting STRING if new STRING text is generated; see Section 29.4.

When a STRING is read from a data file, null characters and carriage returns present in the file are included in the STRING; in other words, data files always act as if the keepNul bit were set, regardless of the setting of that bit when the file was opened.

Data types other than STRING read from a data file are removed from the file; i.e., the file position is updated to be immediately beyond the values read. This means that, in reading a non-STRING variable from a text file, the remainder of the line on which the value occurs is left in the file; for example, the following code fragment:

INTEGER iSTRING s;
...
read(cmdFile,i): read(cmdFile,s);

given the input line (read from cmdFile):

16<eol>

sets i to the value 16, leaving the <eol> in cmdFile, unread. The next call to the textFile/STRING form of read scans up to the (still unread) eol, discards it, and therefore sets s to the empty STRING. Only one line is read from cmdFile for both calls to read, since the first call does not exhaust the input line.

In all reads from a file or STRING, the result is Zero if no value is found when reading from the file or STRING (i.e., if end-of-file or end-of-STRING is encountered).

After reading from an ADDRESS, the ADDRESS is displaced to the location immediately following that from which the value was read. The result is undefined if the ADDRESS is NULLADDRESS, if the ADDRESS is unaligned (see Section 3.9), or if displace(a,size(data type of v)) is undefined.

Note that unlike ADDRESSes, there is no alignment restriction on the current file position when reading from a file; any data type may be read from any position in either a text or a data file.

Example 46–10. Use of read
INTEGER iREAL rBITS b;
...
read(inFile,i,r,b)

reads an INTEGER into i, a REAL into r, and a BITS into b. If inFile is a text file, the file is scanned for the STRING representations; if it is a data file, the proper number of storage units are input.

STRING sINTEGER height,weight;
...
s := "Height is 70 inchesweight 150 pounds.";
read(s,height,weight)

The read picks the first two INTEGERs out of s, thereby shortening s. height becomes 70, weight 150, and s " pounds.".

ADDRESS aINTEGER iREAL r;
...
read(a,i,r)

reads an INTEGER and a REAL starting at memory location a, and updates a to the value given by:

displace(a,size(integerCode) + size(realCode))


previous   next   top   contents   index   framed top   this page unframed

MAINSAIL Language Manual, Section 46.7