[fpc-pascal]EOF function

Thomas Schatzl tom_at_work at gmx.at
Thu Sep 11 22:24:39 CEST 2003


Hi,

> The following (attached) is a programming problem I've implemented.
> Now I know the way I've done the EOF test is not correct, otherwise it
> would not crash with a runtime error of 106.

It doesn't crash here as long as only valid integers are entered. The doc
for RTE 106 reads: "Invalid numeric format: Reported when a non-numeric
value is read from a text file, when a numeric value was expected."

The standard input is never closed during the program - so you will never
get an eof from it (by entering "normal" characters).

> Could someone show me the correct way of reading from stdin until there
> is no more input ?

You have to parse the input yourselves, for example like this (beware:
quick&dirty code and assuming that with "no more input" you mean an empty
line):

procedure ParseString(s : String; var i, j : Integer);
begin
 val(Copy(s, 1, Pos(' ', s)-1), i);
 Delete(s, 1, Pos(' ', s));
 s := TrimLeft(s);
 val(s, j);
 WriteLn(i, ' ', j );
end;

var
 i: longInt;
 j: longInt;
 s : String;
begin
 repeat
   readln(input, s);
   s := TrimLeft(TrimRight(s));
   if (s <> '') then begin
    ParseString(s, i, j);
    writeLn(getMaxCycle(i, j));
   end;
 until (s = '');
end.

This is required when using stdin only - on normal text files the eof is
reported properly and your way of reading in data using read(ln) works. (As
long as the input is correct).

Hth,
  Thomas





More information about the fpc-pascal mailing list