Sequential input/output is input/output
that is performed with sequential files; that is, files whose current
position indexes advance one component at a time. Sequential input
comes from read-only files that the procedure reset
opened. Sequential output goes to write-only files that the procedure
rewrite or append
opened.
Table 3-4 “Characteristics of Sequential I/O Procedures” summarizes the
characteristics of the predefined sequential input/output procedures.
Table 3-4 Characteristics of Sequential I/O Procedures
Procedure | get | read | put | write |
State that file must be in * | Read-only
or read-write | Write-only
or read-write |
Assigns value of | Current
component | Buffer | Specified variable |
To | Buffer | Specified variable | Current
component |
Advances current position index | To next
component ** |
After call, buffer is undefined | No | Yes |
Title not available (Sequential Input/Output )
For sequential I/O, the state must be read-only
or write-only. The state read-write is included here because these
sequential I/O procedures work the same way on direct (read-write)
files (see “Direct Input/Output ”).
For all the procedures except get,
the current position index is advanced to the component after the
assignment. See the explanation of deferred get
that follows this table.
The procedures get
and read assign
values to the buffer with deferred get. Deferred
get allows HP Pascal to maintain the original Pascal definition
of get while
avoiding unexpected behavior with input from interactive I/O devices
(such as terminals).
The procedure get
advances the current position index to the next component and moves
the next component into the buffer variable.
The procedure reset
opens a file for sequential input, positions the file at the first
component, and performs a get.
If the get
(Pascal definition) is performed after a reset
to a terminal, a physical read is required to fill the buffer variable.
Consequently, a program is paused for input from the terminal before
the program requests an input operation.
The deferred get avoids this problem. With deferred get, the
procedure get
advances the current position index to the next component and, on
the next reference to the buffer variable, moves the current component
into the buffer variable. The reference to the buffer variable can
be explicit (f)
or implicit. For example, read(f,v)
or eof(f).
Example 1
 |
PROGRAM prog; TYPE seqfile = FILE OF char; VAR f1,f2,f3 : seqfile; c1,c2 : char; BEGIN reset(f1); {Opens f1 for sequential input. First component of f1 becomes its current component.} c1 := f1; {Assigns f1's first component to f1's buffer. Assigns f1's buffer (first component) to c1.} get(f1); {Advances f1's current position index. Second component of f1 becomes its current component.} read(f1,c2); {Implicit reference to f1's buffer deferred get from get(f1) assigns f1's current (second) component to f1's buffer. Read(f1,c2) assigns f1's current (second) component to c2 and advances f1's current position index. Third component of f1 becomes its current component.} rewrite(f2); {Opens f2 for sequential output (write-only). Erases old contents. Leaves f2's buffer undefined.} get(f2); {Illegal rewrite(f2) made f2 write-only.} f2 := c1; {Assigns c1 to f2's buffer.} put(f2); {Assigns f2's buffer (c1) to f2's current (first) component. Advances f2's current position index to position two, where its second component will be after write(f2,c2).} write(f2,c2); {Assigns c2 to f2's current (second) component. Advances f2's current position index to position three, where its third component will be.} append(f3); {Opens f3 for sequential output (write only). Does not erase old contents, which end with component n. Leaves f3's buffer undefined.} |
 |
(Example is continued on next page.)
get(f3); {Illegal append(f3) made f3 write-only.} f3 := c1; {Assigns c1 to f3's buffer.} put(f3); {Assigns f3's buffer (c1) to f3's current (n+1st) component. Advances f3's current position index to position n+2, where its n+2nd component will be after write(f3,c2).} write(f3,c2); {Assigns c2 to f3's current (n+2nd) component. Advances f3's current position index to position n+3, where its n+3rd component will be.} END. |
The preceding program reads values from the first and second
components of the file f1
into the variables c1
and c2 (respectively).
Then it writes c1
and c2 to the
first and second components of the file f2
(respectively), and appends them to the file f3.
The get
associated with read
is implicit; your program need not call get
explicitly. If it does, a component is skipped.
Example 2
 |
PROGRAM prog; TYPE intfile = FILE OF integer; VAR f : intfile; x,y,z : integer; BEGIN reset(f); {Opens f for sequential input. First component becomes current component.} read(f,x); {Implicit reference to f's buffer deferred get from reset(f), above assigns current (first) component to buffer. Then read(f,x) assigns current (first) component to x. Second component becomes current component.} read(f,y); {Implicit reference to buffer deferred get from read(f,x) assigns current (second) component to buffer. Read(f,y) assigns current (second) component to y and advances current position pointer. Third component becomes current component.} get(f); {Explicit reference to buffer because get(f) follows read(f,y), it advances the current position pointer. Fourth component becomes the current component.} read(f,z); {Implicit reference to buffer deferred get from get(f) assigns current (fourth) component to buffer. Read(f,z) assigns current (fourth) to z. Fifth component becomes the current component.} END. |
 |
The preceding program assigns the first, second, and fourth
components of the file f
to the variables x,
y, and z,
respectively. The program skips the third component.
Table 3-5 “Characteristics of Sequential File Functions” gives the characteristics
of the predefined sequential file functions.
Table 3-5 Characteristics of Sequential File Functions
Function | Eof | Position |
|---|
Returns: | True if the
current position index is at the end-of-file marker; false
otherwise (always true for a write-only
file). | Current position index (an integer). |
Effect on buffer: | If eof returns
false, and the buffer does not have a value, then eof
assigns the value of the current component to the buffer; otherwise,
no effect. | None. |
Trying to read from file f
when eof(f) is
true causes a run-time error. You can prevent
it by calling eof(f)
before attempting to read from f,
and taking appropriate action if eof(f)
is true.
Example 3
PROGRAM prog; TYPE seqfile = FILE OF real; VAR f : seqfile; i : integer; a : ARRAY [1..100] OF real; BEGIN reset(f); {Open f} i := 1; WHILE not eof(f) AND (i<=100) DO {Read array values from f} BEGIN read(f,a[i]); i := i+1; END; END; END. |
If f is a terminal, the appropriate action
for eof is a device read. The next read
or readln of f accesses
the component in the buffer, without performing another device read.
Example 4
PROGRAM prog (input); {for this example, assume input is from terminal} TYPE readbuf = PACKED ARRAY [1..80] OF char; {for device read} VAR x : char; i : 1..100; a : readbuf; BEGIN i := 1; WHILE (NOT eof) AND (i <= 100) DO BEGIN readln(a); {perform device read} i := i + 1; END; END. |
By default, eof
and readln apply
to the standard textfile input.
The user running the program terminates input by pressing RETURN.
An input line can have up to 80 characters.