Sign in Join

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

Mailing lists / fpc-pascal / 2006-04

eof strangeness

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

John Coppens

Hi all.

While modifying some older programs, I found a strange effect with eof (input). When calling eof(input), the program needs an <enter> key to continue.

The definition of eof (according to the FPC docs) is that it immediately returns... which is what I need. Can anyone suggest a solution for this?

program testeof;
begin
  if eof then
    writeln('EOF found')
  else
    writeln('No EOF found');
end.

This sample program doesn't return till <enter> is pressed.

TIA,
John

John Coppens

Hi all.

While modifying some older programs, I found a strange effect with eof (input). When calling eof(input), the program needs an <enter> key to continue. I just downloaded 2.0.2, with the same result.

The definition of eof (according to the FPC docs) is that it immediately returns... which is what I need. Can anyone suggest a solution for this?

program testeof;
begin
  if eof then
    writeln('EOF found')
  else
    writeln('No EOF found');
end.

This sample program doesn't return till <enter> is pressed.

TIA,
John

Note: first send to the list didn't get published...

Bob Richards
to John Coppens: Hi all. While modifying some older programs, I found a strange effect with eof (input).…
John Coppens wrote 10 lines
program testeof; begin if eof then writeln('EOF found') else writeln('No EOF found'); end. This sample program doesn't return till <enter> is pressed.

That's correct and reasonable. You are terminating your program after an EOL (ENTER), If you send it an EOF ^d it also responds reasonably.

$ ./testeof
^d entered at kybd
EOF found

Bob

PGP Encrypted E-Mail Prefered Public Key at: http://www.tamara-b.org/~bob/pubkey.asc .
John Coppens
to Bob Richards
Show quoted text 19 lines
John Coppens wrote: > program testeof; > > begin > if eof then > writeln('EOF found') > else > writeln('No EOF found'); > end. > > This sample program doesn't return till <enter> is pressed. That's correct and reasonable. You are terminating your program after an EOL (ENTER), If you send it an EOF ^d it also responds reasonably. $ ./testeof ^d entered at kybd EOF found

Hi Bob...

I see your reasoning, but I do not understand why eof doesn't return always. The eof is not a 'wait for eof', but a test. If we're _not_ at the end of the file, the program should return with false, at least how I understand it.

program test1;
var
  v: integer;
begin
  if not eof then begin
    write('Wait for input ');
    readln(v);
  end;
end.

the eof call here, makes the program invert it's output, as the eof waits for the buffer to be filled (so the number is already on the screen) then outputs the 'Wait for input' text. It doesn't feel right...

I guess the eof call is superfluous here, but it wouldn't be so illogical if the input channel is redirected (eg. from a file).

Thanks for your input (pun intended ;-)

John

Jonas Maebe
to John Coppens
John Coppens wrote 5 lines
I see your reasoning, but I do not understand why eof doesn't return always. The eof is not a 'wait for eof', but a test. If we're _not_ at the end of the file, the program should return with false, at least how I understand it.

It is not possible to know whether you are at the end of a file or device without (attempting to) read from it (unless you previously hit the eof marker).

Jonas