help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <>
Subject: Re: Weird behavior of Get character with trailing new lines.
Date: Sun, 24 Sep 2023 09:50:48 +0200	[thread overview]
Message-ID: <ueopoo$18vm4$> (raw)
In-Reply-To: <>

On 2023-09-23 16:03, Niklas Holsti wrote:
> On 2023-09-23 12:25, Dmitry A. Kazakov wrote:

>> You exit the loop and do whatever is necessary there.
> That is exactly what happens in the "while not End_Of_File" loop.

It does not because you must handle I/O errors and close the file.

> If you want to use End_Error instead, you have to add an exception 
> handler, and if you want to stay in the subprogram's statement sequence 
> without entering the subprogram-level exception handlers, you have to 
> add a block to contain the reading loop and make the exception handler 
> local to that block.

You always have to in order to handle I/O errors.

> To me that looks like adding code -> more complex. Of course not much 
> more complex, but a little, as I said.

No, it is simpler if the code is production code rather than an 
exercise. Consider typical case when looping implements reading some 
message, block etc. You have

       read something
       read another piece
       read some count
       read a block of count bytes

You cannot do it this way if you use end of file test because you must 
protect each minimal input item (e.g. byte) by the test. It is massively 
obtrusive and would distort program logic. You will end up with nested 
ifs or else gotos.

>> Testing for the file end is unreliable and non-portable. Many types
>> of files simply do not support that test.In other cases the test is
>> not file immutable with the side effects that can change the program
>> logic.
> I suppose you are talking about the need for End_Of_File to possibly 
> read ahead past a line terminator? If not, please clarify.

Yes, reading ahead and also issues with blocking and with race condition 
in shared files. Then things like sockets do not have end of file, 
connection drop is indicated by an empty read.

Dmitry A. Kazakov

  reply	other threads:[~2023-09-24  7:50 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-22 19:30 Weird behavior of Get character with trailing new lines Blady
2023-09-22 19:52 ` Niklas Holsti
2023-09-22 20:05 ` Jeffrey R.Carter
2023-09-23  7:02   ` J-P. Rosen
2023-09-23  8:39     ` Niklas Holsti
2023-09-23  9:25       ` Dmitry A. Kazakov
2023-09-23 14:03         ` Niklas Holsti
2023-09-24  7:50           ` Dmitry A. Kazakov [this message]
2023-09-25 19:55       ` Blady
2023-09-26  5:53     ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox