Textfile Input/Output [ HP Pascal/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP Pascal/iX Programmer's Guide
Textfile Input/Output
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 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 | Read-only | Write-only |
| file must be | | |
| in | | |
| | | |
-----------------------------------------------------------------------------------------------------------
| | | | | | | |
| Writes or | Value of | Specified | End-of-line | Page-eject | Line-feed | Buffer |
| Reads | current | expression | marker | character 3 | suppression | |
| | component | | | | character4 | |
| | | | | | | |
-----------------------------------------------------------------------------------------------------------
| | | | | | |
| To/after | To specified | To current | After | After current component | To output |
| | variable | component | current | | device |
| | | | component | | |
| | | | | | |
-----------------------------------------------------------------------------------------------------------
| | | | | | |
| Advances | To beginning | To beginning of next | To next | To beginning | No |
| current | of next line | line | component | of same line | |
| position | | | | | |
| index | | | | | |
| | | | | | |
-----------------------------------------------------------------------------------------------------------
| | | |
| After call, | No | Yes |
| buffer is | | |
| undefined | | |
| | | |
-----------------------------------------------------------------------------------------------------------
1. 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).
2. writeln and write format the specified variable (see the HP
Pascal/iX Reference Manual or the HP Pascal/HP-UX Reference Manual
for details).
3. The page-eject character causes devices to skip to the top of the
next page when it prints the textfile.
4. 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 | Read-only | Read-only | Write-only |
| be in | | | |
| | | | |
-------------------------------------------------------------------------------------------------
| | | | |
| Returns | True if the current | Number of characters | Number of characters |
| | position index is at | read from file since | written to file since |
| | an end-of-line | last end-of-line | last end-of-line |
| | marker; false | marker (excluding | marker (excluding |
| | otherwise. | character in buffer). | character in buffer). |
| | | | |
| | | | |
| | | After readln, or when | After writeln, or |
| | | current position | when current position |
| | | index is at | index is at |
| | | end-of-line marker, | end-of-line marker, |
| | | this number is zero. | this number is zero. |
| | | | |
-------------------------------------------------------------------------------------------------
| | | |
| Effect on buffer | If eoln returns true, | None |
| | it assigns a blank | |
| | character to the | |
| | buffer | |
| | | |
-------------------------------------------------------------------------------------------------
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.
MPE/iX 5.0 Documentation