 |
» |
|
|
 |
Textfile input/output is sequential input/output
that is performed with textfiles (a subset of sequential files).
The program reads textfile input from read-only textfiles opened
by the procedure reset,
or from the standard textfile input. The program
writes textfile output to write-only textfiles opened by the procedure
rewrite or append,
or to the standard textfile output. Table 3-6 “Characteristics of Textfile I/O Procedures” summarizes the
characteristics of the predefined textfile input/output procedures. Table 3-6 Characteristics of Textfile I/O Procedures Procedure | readln1 | writeln2 | page | overprint | prompt | State that file must be in | Read-only | Write-only | Writes or Reads | Value of current component | Specified expression | End-of-line marker | Page-eject character 3 | Line-feed suppression character4 | Buffer | To/after | To specified variable | To current component | After current component | After current
component | To output device | Advances current position index | To beginning of next line | To beginning
of next line | To next component | To beginning of same line | No | After call, buffer is undefined | No | Yes |
readln
and read perform
implicit data conversion if the specified variable is of any simple
type other than char (see the HP
Pascal/iX Reference Manual or the HP Pascal/HP-UX
Reference Manual for details). writeln
and write format
the specified variable (see the HP Pascal/iX Reference
Manual or the HP Pascal/HP-UX Reference Manual
for details). The page-eject character causes devices to skip
to the top of the next page when it prints the textfile. The line-feed suppression character prevents the
device from moving to the next line after it prints the parameter
of overprint; thus the sequence overprint('ABC'); writeln('XYZ');'' |
prints ABC
and then prints XYZ
on top of it.
The file-opening procedures rewrite
and append and
the textfile output procedures writeln,
page, overprint,
and prompt leave
the buffer undefined. Example 1 PROGRAM prog (in,out); VAR in,out : text; w,x,y,z : char; BEGIN reset(in); {Open in for textfile input} rewrite(out); {Open out for textfile output} readln(in,x,y,z); {Read x, y, and z from in} write(out,x); {Write x to out} overprint(out); {Write buffer and line-feed suppression to out} writeln(out,y); {Write y to out and advance to next line} page(out); {Write page-eject character to out} writeln(out,z); {Write z to out and advance to next line} prompt(out,'?'); {Write '?' to out, without carriage control} readln(in,w); {Read user's answer to '?' from in} writeln(out,w); {Write user's answer to out} END. |
When a device prints the file out, it
prints the value of y
over the value of x,
and it prints the values of z
and w on the
next page. Table 3-7 summarizes the characteristics of the predefined
textfile functions. Table 3-7 Characteristics of Textfile Functions Function | Eoln | Linepos | State that file must be in | Read-only | Read-only | Write-only | Returns | True if the
current position index is at an end-of-line marker; false
otherwise. | Number of characters read from file since
last end-of-line marker (excluding character in buffer). After readln,
or when current position index is at end-of-line marker, this number
is zero. | Number of characters written to file
since last end-of-line marker (excluding character in buffer). After
writeln, or when current position index
is at end-of-line marker, this number is zero. | Effect on buffer | If eoln returns
true, it assigns a blank character to
the buffer | None |
Example 2 PROGRAM prog (infile,outfile,output); VAR infile, outfile : text; i : integer; c : char; BEGIN reset(infile); {Open infile for input} rewrite(outfile); {Open outfile for output} WHILE not(eof(infile)) DO BEGIN {If infile is not at end-of-file} IF eoln(infile) THEN BEGIN {but the current line of in has ended,} writeln(linepos(infile)); {print the number of characters read from the current line of infile,} readln(infile); {and advance to the next line.} writeln(linepos(outfile)); {Also, print the number of characters written to outfile,} writeln(outfile); {and start a new line of outfile.} END {IF} {If the current line of infile has not ended,} ELSE BEGIN read(in,c); {read the next character of infile,} write(out,c); {and write it to outfile.} END; END; {WHILE} END. |
The preceding program copies the textfile
infile to the
textfile outfile,
writing the values of linepos(infile)
and linepos(outfile)
to the standard textfile output
whenever eoln(infile)
is true.
Except for the position function, every
sequential I/O procedure and sequential file function applies to
textfiles (see “Sequential Input/Output ”). Sequential
files work the same way, except that for textfiles, read
(like readln)
sometimes performs implicit data conversion, and write
(like writeln)
can format the output value. See the HP Pascal/iX Reference
Manual or the HP Pascal/HP-UX Reference Manual,
depending on your implementation, for information on implicit data
conversion and formatting output values.
|