• Keine Ergebnisse gefunden

TEXT FILE INPUT AND OUTPUT

Im Dokument INTRODUCTION AND FEATURES (Seite 197-200)

Human-readable input and output in standard Pascal are done with textfiles. Textfiles are files of type TEXT and always have ASCII structure. Normally, the standard textfiles INPUT and OUTPUT are given as program parameters in the PROGRAM heading:

PROGRAM IN_AND_OUT (INPUT,OUTPUT);

Other textfiles usually represent some input or output device such as a terminal, a card reader, a line printer, or an opera ting system disk file. The extended level permits using additional files not given as program parameters. In order to facilitate the handling of textfiles, the four standard proced ures READ, READLN, WRITE, and WRITELN are provided in addition to the procedures GET and PUT.

These procedures are more flexible in the syntax for their parameter lists, allowing, among other things, for a variable number of parameters. Moreover, the parameters need not necessarily be of type CHAR, but can also be of certain other types, in which case the data transfer is accompanied by an implicit data conversion operation. In some cases, parameters can include additional formatting values that affect the data conversions used.

If the first variable is a file variable, then it is the file to be read or written. Otherwise, the standard files INPUT and OUTPUT are automatically assumed as default values in the cases of reading and writing, respectively.

These two files have TERMINAL mode and ASCII structure and are predeclared as:

VAR INPUT, OUTPUT: TEXT;

The files INPUT and OUTPUT are treated like other textfiles.

They can be used wi th ASSIGN, CLOSE, RESET, REWRITE, and the other procedures and functions. However, even if present as program parameters, they are not initialized with a filename.

Instead, they are assigned to the user's terminal. RESET of INPUT and REWRITE of OUTPUT are done automatically, whether or not they are present as program parameters.

Textfiles represent a special case among file types insofar as the y are s t r u c t u red in to 1 i n e s by" 1 in e mar k e r s" • If, u po n reading a textfile F, the file position is advanced to a line marker (i.e., past the last character of a line), then the value of the buffer var iable F.... becomes a blank, and the standard function EOLN (F) yields the value true. For example:

+---+---+---+---+---+---+---+---+---+---+---+

I'L'I'I'I'N'I'E'I'O'I'F'I'T'I'E'I'x'I'T'1 I +---+---+---+---+---+---+---+---+---+---+---+

{EOLN TRUE} {F .... I }

Advancing the file position once more caus~s one of three things to happen:

1. If the end of the ffle is reached, then EOF (F) becomes TRUE.

2. If the next line is empty, a blank is assigned to F .... and EOLN (F) remains TRUE.

3. Otherwise, the first character of the next line is assigned to F .... and EOLN (F) is set to FALSE.

Since line markers are not elements of type CHAR in standard Pascal, they can, in theory, only be generated by the procedure WRITELN". However, in this Pascal, an actual character may be used for the line marker. It may therefore be possible to WRITE a line marker, but not to READ one.

When a textfile being written is closed, a final line marker is automatically appended to the last line of any nonempty file in which the last character is not already a line marker • .

When a textfile being read reaches the end of a nonempty file, a line marker for the last line is returned even if one was not"

present in the file. Therefore, lines in a textfile always end with a line marker.

Any list of data written by a WRITELN is usually readable with the same list in a READLN (unless an LSTRING occurs that is not on the end of the list.)

Interactive prompt and response is very easy in Pascal. To have input on the same line as the response, use WRITE for the prompt.

READLN must always be used for the response. For example:

WRITE ('Enter command: ');

READLN (response);

If no file is given, most of the textfile procedures and functions assume either the INPUT file or the OUTPUT file. For example, if I is of type INTEGER, then READ (I) is the same as READ (INPUT, I).

READ and READLN

PROCEDURE READ PROCEDURE READLN

READ and READLN read data from textfiles. Both are defined in terms of the more primitive operation, GET. That is, if P is of type CHAR, then READ (F, P) is equivalent to:

BEGIN

P :

=

F .... ; GET (F) END

{Assign buffer variable F .... to P.}

{Assign next compone~t of file to F ... }

READ can take more than a single parameter, as in READ (F, PI, P2, ••• Pn). This is equivalent to the following:

BEGIN

READ (F, PI);

READ (F , P 2) ; READ (F, pn) END

The procedure READLN is very much like READ, except that it reads up to and including the end-of-line. At the primitive GET level, without parameters, READLN is equivalent to the following:

BEGIN END

WHILE NOT EOLN (F) DO GET (F);

GET (F)

A READLN with parameters, as in READLN (F, PI, P2, ••• Pn), is equivalent to the following:

BEGIN

END

READ (F, PI, P2, pn);

READLN (F)

READLN is often used to skip to the beg inning of the next line.

It can only be used with textfiles (ASCII mode).

If no other file is specified, both READ and READLN read from the standard INPUT file. Therefore, the name INPUT need not be designated explicitly. For example, these two READ statements

perform identical actions:

READ (PI, P2, P3)

READ (INPUT, PI, P2, P3) {Reads INPUT by default}

At the standard level, parameters PI, P2, and P3 above must be of one of the following types:

CHAR INTEGER REAL

The extended level also allows READ var iables of the following types:

WORD

an enumerated type BOOLEAN

INTEGER4

a pointer type STRING

LSTRING

When the compiler reads a variable of a subrange type, the value read must be in range. Otherwise, an error occurs, regardless of the setting of the range checking switch.

The procedure READ can also read from a file that is not a textfile (e.g., has BINARY IIl()de). The form READ (F, PI, P2, •••

Pn) can be used on a BIlU3R~ file. However, this READ will not work as expected after a SEEK on a DIRECT mode file. For BINARY files, READ (F, X) is equivalent to:

BEGIN

X :

=

FA;

GET (F) END

Im Dokument INTRODUCTION AND FEATURES (Seite 197-200)