[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
READ Input Data to Variable pp 108
Syntax: Read (FileVar, Var1, Var2,..VarN) ;
Read (Var1, Var2,..VarN) ;
Type: ALL
Form: Procedure
Purpose: Input characters, strings or numeric data to variables.
Notes: Variable types may be Char, Byte, Integer, Real, String.
Char:
With a variable type of Char, Read reads on character from a
file and assigns it to the variable.
If the file is a disk file then Eoln is true if the next
character is a CR or Ctrl-Z. Eof is also true if Ctrl-Z
or physical end of file is reached.
If the file is a logical device such as CON: or the standard
files Input and Output, then Eoln is true if the character read
was a CR or if Eof is true. Eof will be true if the character
read was a Ctrl-Z.
String:
Read will read the number of characters defined by the maximum
length of the string variable unless Eoln or Eof is reached first.
Eoln and Eof apply in the same manner as type Char.
Numeric:
Read expects a stream of charcters which complies with the
format of the defined numeric variable. Any tabs, blanks, CR's,
or LF's preceeding the stream are skipped. The stream must be
no longer than 30 characters, and must be followed by a blank, tab,
CR, or Ctrl-Z, or else an I/O error occurs. The stream is converted
to a value of the approprate type and assigned to the variable.
When reading from a disk file and the input string is ended with a
blank or tab, the next Read or ReadLn will start with the
character immediately following the blank or tab character. If the
first charcter in the numeric string is a CR, then the variable is
left unchanged and retains its former value.
Console Input:
If the input file is assigned to the console device CON: or
the standard file Input is used in the default {$B+} mode then
special rules apply to the reading of variables. For a call to
Read or ReadLn a line is input from the console and stored
in a buffer, and the reading of variables uses this buffer as the
input source. This allows for editing during data entry.
Console Editing:
The Backspace key or Ctrl-H backs the cursor one position to
the left and erases the character under the cursor.
Esc and Ctrl-X erases all characters back to the start of the line.
Ctrl-D recalls one character from the last input line.
Ctrl-R recalls the last input line.
Return and Ctrl-M terminates the input line and stores the data
into the line buffer along with a CR/LF terminator. The CR/LF
is not echoed to the screen.
Ctrl-Z terminates the input line and stores and end of file marker
($1A) in the line buffer. The line buffer is terminated with the
Eof marker. If fewer values are specified on the input line than
the number of variables in the Read parameter list then excess
Char variables will be set to Eof, strings will be empty, and
numeric variables will remain unchanged.
The maximum number of characters that can be entered on an input
line from the console is 127 by default. This may be lowered by
changing the predefined variable BufLen from 0..127.
Assignments to BufLen affect only the following Read.
After the read, BufLen is restored to the default value of 127.
Usage:
TYPE
FileType = String [13] ; { File record size = 13 }
HeapPtr = ^FileType ; { Define heap pointer }
DataFile = Record
Name : String [10] ; { 10 + length byte = 11 }
Age : Byte ; { 1 }
Sex : Char ; { 1 }
End ; { 13 byte record }
VAR
FileVar : File of FileType ; { FileVar is 13 byte record }
FileName : String [14] ; { File name }
HeapRec : HeapPtr ; { Define heap record type }
Person : DataFile ; { Assign person to record type }
BEGIN
Assign (FileVar,FileName) ; { Assign file name }
Reset (FileVar) ; { Open file handle }
New (HeapRec) ; { Create record space on heap }
Read (FileVar,HeapRec^) ; { Read formatted record to heap }
WriteLn (Person.Name) ; { Record name field }
WriteLn (Person.Age) ; { Record age field }
WriteLn (Person.Sex) ; { Record sex field }
Close (FileVar) ; { Close the handle }
END.
See Also:
Eof
Eoln
ReadLn
Write
WriteLn
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson