Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-09

EOF function

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Mills

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.

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

cheers
James

- -Zero Defect Software Engineers Group - ZDSEG - -You need only two tools. WD-40 and duct tape. -If it doesn't move and it should, use WD-40. -If it moves and shouldn't, use the tape.
Maciej Łaszcz
to James Mills
James Mills wrote 8 lines
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. Could someone show me the correct way of reading from stdin until there is no more input ?

Have you tried to change the read to readln? IMO you are reading EOLN to the integer.

--[ ]--[ GG 1333 (thanks to gophi :*) ]-- --[ Nie chcę mieć słodkiej sygnaturki, ale i tak mam ]--
James Mills
to Maciej Łaszcz
Maciej ?aszcz wrote 13 lines
On Thu, 11 Sep 2003, James Mills wrote: > 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. > > Could someone show me the correct way of reading from stdin until there > is no more input ? Have you tried to change the read to readln? IMO you are reading EOLN to the integer.

Well if you look at the input file it containts:

1 2
3 5
100 200

... etc

Would you still be able to do: readLn(input, i, j); ?

cheers
James

- -Zero Defect Software Engineers Group - ZDSEG - -You need only two tools. WD-40 and duct tape. -If it doesn't move and it should, use WD-40. -If it moves and shouldn't, use the tape.
Thomas Schatzl
to James Mills: The following (attached) is a programming problem I've implemented. Now I know the way…

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