Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"When people are least sure, they are often most dogmatic." -- John Kenneth Galbraith


devel / comp.lang.ada / Re: Weird behavior of Get character with trailing new lines.

SubjectAuthor
* Weird behavior of Get character with trailing new lines.Blady
+- Re: Weird behavior of Get character with trailing new lines.Niklas Holsti
`* Re: Weird behavior of Get character with trailing new lines.Jeffrey R.Carter
 `* Re: Weird behavior of Get character with trailing new lines.J-P. Rosen
  +* Re: Weird behavior of Get character with trailing new lines.Niklas Holsti
  |+* Re: Weird behavior of Get character with trailing new lines.Dmitry A. Kazakov
  ||`* Re: Weird behavior of Get character with trailing new lines.Niklas Holsti
  || `- Re: Weird behavior of Get character with trailing new lines.Dmitry A. Kazakov
  |`- Re: Weird behavior of Get character with trailing new lines.Blady
  `- Re: Weird behavior of Get character with trailing new lines.Randy Brukardt

1
Weird behavior of Get character with trailing new lines.

<uekq08$clvb$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=10094&group=comp.lang.ada#10094

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: p.p11@orange.fr (Blady)
Newsgroups: comp.lang.ada
Subject: Weird behavior of Get character with trailing new lines.
Date: Fri, 22 Sep 2023 21:30:15 +0200
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <uekq08$clvb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 22 Sep 2023 19:30:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="1ea2f90b6038eae0d113dfd19a1f3943";
logging-data="415723"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18yfdM6+p+1ebmmYMtj+cNA"
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0)
Gecko/20100101 Thunderbird/102.15.1
Cancel-Lock: sha1:VrxHpR1eublGcs5Bk7CIEt3Q/uc=
Content-Language: fr, en-US
 by: Blady - Fri, 22 Sep 2023 19:30 UTC

Hello,

I'm reading a text file with Get character from Text_IO with a while
loop controlled by End_Of_File.

% cat test_20230922_get_char.adb
with Ada.Text_IO; use Ada.Text_IO;
procedure test_20230922_get_char is
procedure Get is
F : File_Type;
Ch : Character;
begin
Open (F, In_File, "test_20230922_get_char.adb");
while not End_Of_File(F) loop
Get (F, Ch);
Put (Ch);
end loop;
Close (F);
Put_Line ("File read with get.");
end;
begin
Get;
end;

All will be well, unfortunately not!

Despite the End_Of_File, I got an END_ERROR exception when there are
several trailing new lines at the end of the text:

% test_20230922_get_char
with Ada.Text_IO; use Ada.Text_IO;procedure test_20230922_get_char is
procedure Get is F : File_Type; Ch : Character; begin
Open (F, In_File, "test_20230922_get_char.adb"); while not
End_Of_File(F) loop Get (F, Ch); Put (Ch); end
loop; Close (F); Put_Line ("File read with get.");
end;beginGet;end;

Execution of ../bin/test_20230922_get_char terminated by unhandled exception
raised ADA.IO_EXCEPTIONS.END_ERROR : a-textio.adb:517

The code is compiled with GNAT, does it comply with the standard?

A.10.7 Input-Output of Characters and Strings
For an item of type Character the following procedures are provided:
procedure Get(File : in File_Type; Item : out Character);
procedure Get(Item : out Character);
After skipping any line terminators and any page terminators, reads the
next character from the specified input file and returns the value of
this character in the out parameter Item.
The exception End_Error is propagated if an attempt is made to skip a
file terminator.

This seems to be the case, then how to avoid the exception?

Thanks, Pascal.

Re: Weird behavior of Get character with trailing new lines.

<kn69jlFq9j6U1@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=10095&group=comp.lang.ada#10095

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: niklas.holsti@tidorum.invalid (Niklas Holsti)
Newsgroups: comp.lang.ada
Subject: Re: Weird behavior of Get character with trailing new lines.
Date: Fri, 22 Sep 2023 22:52:21 +0300
Organization: Tidorum Ltd
Lines: 75
Message-ID: <kn69jlFq9j6U1@mid.individual.net>
References: <uekq08$clvb$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net WlHU6dS2N9DNwLp33WZKzwpMS+0AsxASSWK2x8g/ilqq2wYkGR
Cancel-Lock: sha1:A3Fo7Yv8544ptIyxKAdRS+ErFdg= sha256:9uctbmxCWL8SmAzA5eal4rRqDg6y9oG9snRzShcFV0g=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:102.0)
Gecko/20100101 Thunderbird/102.12.0
Content-Language: en-US
In-Reply-To: <uekq08$clvb$1@dont-email.me>
 by: Niklas Holsti - Fri, 22 Sep 2023 19:52 UTC

On 2023-09-22 22:30, Blady wrote:
> Hello,
>
> I'm reading a text file with Get character from Text_IO with a while
> loop controlled by End_Of_File.
>
> % cat test_20230922_get_char.adb
> with Ada.Text_IO; use Ada.Text_IO;
> procedure test_20230922_get_char is
>    procedure Get is
>       F : File_Type;
>       Ch : Character;
>    begin
>       Open (F, In_File, "test_20230922_get_char.adb");
>       while not End_Of_File(F) loop
>          Get (F, Ch);
>          Put (Ch);
>       end loop;
>       Close (F);
>       Put_Line ("File read with get.");
>    end;
> begin
> Get;
> end;
>
>
>
> All will be well, unfortunately not!
>
> Despite the End_Of_File, I got an END_ERROR exception when there are
> several trailing new lines at the end of the text:
>
> % test_20230922_get_char
> with Ada.Text_IO; use Ada.Text_IO;procedure test_20230922_get_char is
> procedure Get is      F : File_Type;      Ch : Character;   begin Open
> (F, In_File, "test_20230922_get_char.adb");      while not
> End_Of_File(F) loop         Get (F, Ch);         Put (Ch);      end
> loop;      Close (F);      Put_Line ("File read with get.");
> end;beginGet;end;
>
> Execution of ../bin/test_20230922_get_char terminated by unhandled
> exception
> raised ADA.IO_EXCEPTIONS.END_ERROR : a-textio.adb:517
>
> The code is compiled with GNAT, does it comply with the standard?
>
> A.10.7 Input-Output of Characters and Strings
> For an item of type Character the following procedures are provided:
> procedure Get(File : in File_Type; Item : out Character);
> procedure Get(Item : out Character);
> After skipping any line terminators and any page terminators, reads the
> next character from the specified input file and returns the value of
> this character in the out parameter Item.
> The exception End_Error is propagated if an attempt is made to skip a
> file terminator.
>
> This seems to be the case, then how to avoid the exception?

In Text_IO, a line terminator is not an ordinary character, so you must
handle it separately, for example like this:

while not End_Of_File(F) loop
if End_Of_Line(F) then
New_Line;
Skip_Line(F);
else
Get (F, Ch);
Put (Ch);
end if;

Re: Weird behavior of Get character with trailing new lines.

<ueks33$82qd$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=10097&group=comp.lang.ada#10097

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: spam.jrcarter.not@spam.acm.org.not (Jeffrey R.Carter)
Newsgroups: comp.lang.ada
Subject: Re: Weird behavior of Get character with trailing new lines.
Date: Fri, 22 Sep 2023 22:05:55 +0200
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <ueks33$82qd$1@dont-email.me>
References: <uekq08$clvb$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 22 Sep 2023 20:05:56 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bfeeae48422f80c6870c9baeced2c108";
logging-data="265037"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+d08A3EwensA2ctlqwP4o8TWe/TeSyvI8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:MEPLJmvZjqLmXR58PZMxMHbxC5Y=
Content-Language: en-US
In-Reply-To: <uekq08$clvb$1@dont-email.me>
 by: Jeffrey R.Carter - Fri, 22 Sep 2023 20:05 UTC

On 2023-09-22 21:30, Blady wrote:
>
> A.10.7 Input-Output of Characters and Strings
> For an item of type Character the following procedures are provided:
> procedure Get(File : in File_Type; Item : out Character);
> procedure Get(Item : out Character);
> After skipping any line terminators and any page terminators, reads the next
> character from the specified input file and returns the value of this character
> in the out parameter Item.
> The exception End_Error is propagated if an attempt is made to skip a file
> terminator.

As you have quoted, Get (Character) skips line terminators. End_Of_File returns
True if there is a single line terminator before the file terminator, but False
if there are multiple line terminators before the file terminator. So you either
have to explicitly skip line terminators, or handle End_Error.

--
Jeff Carter
"Unix and C are the ultimate computer viruses."
Richard Gabriel
99

Re: Weird behavior of Get character with trailing new lines.

<uem2id$moia$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=10098&group=comp.lang.ada#10098

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: rosen@adalog.fr (J-P. Rosen)
Newsgroups: comp.lang.ada
Subject: Re: Weird behavior of Get character with trailing new lines.
Date: Sat, 23 Sep 2023 09:02:37 +0200
Organization: Adalog
Lines: 30
Message-ID: <uem2id$moia$1@dont-email.me>
References: <uekq08$clvb$1@dont-email.me> <ueks33$82qd$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 23 Sep 2023 07:02:38 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a921a0f1aeaed0f13dc78d6902098417";
logging-data="746058"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19uXR2Eg4Q/yU8QwDUvQxX2"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:l3wxHkNIF90+eISY5eaJOA1uI/g=
Content-Language: en-US, fr
In-Reply-To: <ueks33$82qd$1@dont-email.me>
 by: J-P. Rosen - Sat, 23 Sep 2023 07:02 UTC

Le 22/09/2023 à 22:05, Jeffrey R.Carter a écrit :
> On 2023-09-22 21:30, Blady wrote:
>>
>> A.10.7 Input-Output of Characters and Strings
>> For an item of type Character the following procedures are provided:
>> procedure Get(File : in File_Type; Item : out Character);
>> procedure Get(Item : out Character);
>> After skipping any line terminators and any page terminators, reads
>> the next character from the specified input file and returns the value
>> of this character in the out parameter Item.
>> The exception End_Error is propagated if an attempt is made to skip a
>> file terminator.
>
> As you have quoted, Get (Character) skips line terminators. End_Of_File
> returns True if there is a single line terminator before the file
> terminator, but False if there are multiple line terminators before the
> file terminator. So you either have to explicitly skip line terminators,
> or handle End_Error.
>
And this works only if the input file is "well formed", i.e. if it has
line terminators as the compiler expects them to be (f.e., you will be
in trouble if the last line has no LF).
That's why I never check End_Of_File, but handle the End_Error
exception. It always works.
--
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
https://www.adalog.fr https://www.adacontrol.fr

Re: Weird behavior of Get character with trailing new lines.

<kn7mhtF58psU1@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=10099&group=comp.lang.ada#10099

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: niklas.holsti@tidorum.invalid (Niklas Holsti)
Newsgroups: comp.lang.ada
Subject: Re: Weird behavior of Get character with trailing new lines.
Date: Sat, 23 Sep 2023 11:39:25 +0300
Organization: Tidorum Ltd
Lines: 48
Message-ID: <kn7mhtF58psU1@mid.individual.net>
References: <uekq08$clvb$1@dont-email.me> <ueks33$82qd$1@dont-email.me>
<uem2id$moia$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net DqoT8syn6j0Hh93BWVChAwWFC5espPaTxWzl3IoFlfVbBCw+RB
Cancel-Lock: sha1:r3aaNkcs44eNpF37GH8kKCw2prQ= sha256:fgmZUabWVs3DefhjNbl9isALNN4A4mHTZ6JxmloRwEM=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:102.0)
Gecko/20100101 Thunderbird/102.12.0
Content-Language: en-US
In-Reply-To: <uem2id$moia$1@dont-email.me>
 by: Niklas Holsti - Sat, 23 Sep 2023 08:39 UTC

On 2023-09-23 10:02, J-P. Rosen wrote:
> Le 22/09/2023 à 22:05, Jeffrey R.Carter a écrit :
>> On 2023-09-22 21:30, Blady wrote:
>>>
>>> A.10.7 Input-Output of Characters and Strings
>>> For an item of type Character the following procedures are provided:
>>> procedure Get(File : in File_Type; Item : out Character);
>>> procedure Get(Item : out Character);
>>> After skipping any line terminators and any page terminators, reads
>>> the next character from the specified input file and returns the
>>> value of this character in the out parameter Item.
>>> The exception End_Error is propagated if an attempt is made to skip a
>>> file terminator.
>>
>> As you have quoted, Get (Character) skips line terminators.
>> End_Of_File returns True if there is a single line terminator before
>> the file terminator, but False if there are multiple line terminators
>> before the file terminator. So you either have to explicitly skip line
>> terminators, or handle End_Error.
>>
> And this works only if the input file is "well formed", i.e. if it has
> line terminators as the compiler expects them to be (f.e., you will be
> in trouble if the last line has no LF).

Hm. The code I suggested, which handles line terminators separately,
does work without raising End_Error even if the last line has no line
terminator, at least in the context of the OP's program.

> That's why I never check End_Of_File, but handle the End_Error
> exception. It always works.

True, but it may not be convenient for the overall logic of the program
that reads the file. That program often wants do to something with the
contents, after reading the whole file, and having to enter that part of
the program through an exception does complicate the code a little.

On the other hand, past posts on this issue say that using End_Error
instead of the End_Of_File function is faster, probably because the
Text_IO code that implements Get cannot know that the program has
already checked for End_Of_File, so Get has to check for that case
anyway, redundantly.

My usual method for reading text files is to use Text_IO.Get_Line, and
(I admit) usually with End_Error termination.

Re: Weird behavior of Get character with trailing new lines.

<uematg$o2i1$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=10100&group=comp.lang.ada#10100

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!rocksolid2!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: mailbox@dmitry-kazakov.de (Dmitry A. Kazakov)
Newsgroups: comp.lang.ada
Subject: Re: Weird behavior of Get character with trailing new lines.
Date: Sat, 23 Sep 2023 11:25:05 +0200
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <uematg$o2i1$1@dont-email.me>
References: <uekq08$clvb$1@dont-email.me> <ueks33$82qd$1@dont-email.me>
<uem2id$moia$1@dont-email.me> <kn7mhtF58psU1@mid.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 23 Sep 2023 09:25:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="2bb8be0c39d57643ddd8a84a07cf5fc3";
logging-data="789057"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/oQzQW+5k39nGbrzppiNe1K2nFdnr5tWU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:1eHYsC9ICW+PavnyMEoThIYrLN8=
Content-Language: en-US
In-Reply-To: <kn7mhtF58psU1@mid.individual.net>
 by: Dmitry A. Kazakov - Sat, 23 Sep 2023 09:25 UTC

On 2023-09-23 10:39, Niklas Holsti wrote:
> On 2023-09-23 10:02, J-P. Rosen wrote:

>> That's why I never check End_Of_File, but handle the End_Error
>> exception. It always works.
>
> True, but it may not be convenient for the overall logic of the program
> that reads the file. That program often wants do to something with the
> contents, after reading the whole file, and having to enter that part of
> the program through an exception does complicate the code a little.

It rather simplifies the code. You exit the loop and do whatever is
necessary there.

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.

It is well advised to never ever use it.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

Re: Weird behavior of Get character with trailing new lines.

<kn89h2F64t4U1@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=10104&group=comp.lang.ada#10104

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: niklas.holsti@tidorum.invalid (Niklas Holsti)
Newsgroups: comp.lang.ada
Subject: Re: Weird behavior of Get character with trailing new lines.
Date: Sat, 23 Sep 2023 17:03:14 +0300
Organization: Tidorum Ltd
Lines: 45
Message-ID: <kn89h2F64t4U1@mid.individual.net>
References: <uekq08$clvb$1@dont-email.me> <ueks33$82qd$1@dont-email.me>
<uem2id$moia$1@dont-email.me> <kn7mhtF58psU1@mid.individual.net>
<uematg$o2i1$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net KVzprfElTXaOu35CNdLaPQDSgbrW0tj4bSDKWrs//+/CT7fy3j
Cancel-Lock: sha1:q3Cb7XbG1WJlPAGWu+VPfBFTsm0= sha256:RtmmzRCOtH0q9TIbrfzYKX8PQVK2Ft8Bk5paYqyujYo=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:102.0)
Gecko/20100101 Thunderbird/102.12.0
Content-Language: en-US
In-Reply-To: <uematg$o2i1$1@dont-email.me>
 by: Niklas Holsti - Sat, 23 Sep 2023 14:03 UTC

On 2023-09-23 12:25, Dmitry A. Kazakov wrote:
> On 2023-09-23 10:39, Niklas Holsti wrote:
>> On 2023-09-23 10:02, J-P. Rosen wrote:
>
>>> That's why I never check End_Of_File, but handle the End_Error
>>> exception. It always works.
>>
>> True, but it may not be convenient for the overall logic of the
>> program that reads the file. That program often wants do to something
>> with the contents, after reading the whole file, and having to enter
>> that part of the program through an exception does complicate the code
>> a little.
>
> It rather simplifies the code.

Oh?

> You exit the loop and do whatever is necessary there.

That is exactly what happens in the "while not End_Of_File" loop.

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.

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

> 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.

That said, I certainly think that a program reading files should be
prepared to handle End_Error, especially if a file is read at several
places in the program (and not in a single loop as in the present program).

Re: Weird behavior of Get character with trailing new lines.

<ueopoo$18vm4$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=10110&group=comp.lang.ada#10110

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: mailbox@dmitry-kazakov.de (Dmitry A. Kazakov)
Newsgroups: comp.lang.ada
Subject: Re: Weird behavior of Get character with trailing new lines.
Date: Sun, 24 Sep 2023 09:50:48 +0200
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <ueopoo$18vm4$1@dont-email.me>
References: <uekq08$clvb$1@dont-email.me> <ueks33$82qd$1@dont-email.me>
<uem2id$moia$1@dont-email.me> <kn7mhtF58psU1@mid.individual.net>
<uematg$o2i1$1@dont-email.me> <kn89h2F64t4U1@mid.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 24 Sep 2023 07:50:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ea8d2897c5498918a6f79f457b09b917";
logging-data="1343172"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19sZtLQtgwrJgItrywY29xF+aCqF/pO6EA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:RaQ4kVDxwzSV+eWLpaCtOvwDd88=
Content-Language: en-US
In-Reply-To: <kn89h2F64t4U1@mid.individual.net>
 by: Dmitry A. Kazakov - Sun, 24 Sep 2023 07:50 UTC

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

loop
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.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

Re: Weird behavior of Get character with trailing new lines.

<uesokd$231a1$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=10123&group=comp.lang.ada#10123

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: p.p11@orange.fr (Blady)
Newsgroups: comp.lang.ada
Subject: Re: Weird behavior of Get character with trailing new lines.
Date: Mon, 25 Sep 2023 21:55:56 +0200
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <uesokd$231a1$1@dont-email.me>
References: <uekq08$clvb$1@dont-email.me> <ueks33$82qd$1@dont-email.me>
<uem2id$moia$1@dont-email.me> <kn7mhtF58psU1@mid.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 25 Sep 2023 19:55:58 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="cc4641d4419fd484928a042cceb1e692";
logging-data="2196801"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+rps4Dl6cUsxZ+Xx40W1ft"
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0)
Gecko/20100101 Thunderbird/102.15.1
Cancel-Lock: sha1:07oY/O78oPBWDnmoImppPXh6cew=
Content-Language: fr, en-US
In-Reply-To: <kn7mhtF58psU1@mid.individual.net>
 by: Blady - Mon, 25 Sep 2023 19:55 UTC

Le 24/09/2023 à 09:50, Dmitry A. Kazakov a écrit :
> On 2023-09-23 16:03, Niklas Holsti wrote:
>> On 2023-09-23 10:02, J-P. Rosen wrote: >>> Le 22/09/2023 à 22:05, Jeffrey R.Carter a écrit :>>>> On 2023-09-22
21:30, Blady wrote:
>>>>
>>>> A.10.7 Input-Output of Characters and Strings
>>>> For an item of type Character the following procedures are provided:
>>>> procedure Get(File : in File_Type; Item : out Character);
>>>> procedure Get(Item : out Character);
>>>> After skipping any line terminators and any page terminators, reads
>>>> the next character from the specified input file and returns the
>>>> value of this character in the out parameter Item.
>>>> The exception End_Error is propagated if an attempt is made to skip
>>>> a file terminator.

Thanks all for your helpful answers.

It actually helps.

Especially, I was not aware of the particular behavior of End_Of_File
with a single line terminator before the file terminator.

In my case, I prefer to reserve exceptions for exceptional situations
:-) so I've took the code from Niklas example.

Regards, Pascal.

Re: Weird behavior of Get character with trailing new lines.

<uetrko$2cb0h$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=10126&group=comp.lang.ada#10126

  copy link   Newsgroups: comp.lang.ada
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: randy@rrsoftware.com (Randy Brukardt)
Newsgroups: comp.lang.ada
Subject: Re: Weird behavior of Get character with trailing new lines.
Date: Tue, 26 Sep 2023 00:53:53 -0500
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <uetrko$2cb0h$1@dont-email.me>
References: <uekq08$clvb$1@dont-email.me> <ueks33$82qd$1@dont-email.me> <uem2id$moia$1@dont-email.me>
Injection-Date: Tue, 26 Sep 2023 05:53:29 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f0a5cbfd923c9b47a54100ea42585311";
logging-data="2501649"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18v4kuyb2P3JQqDqC5X6Otr83la59dbw6I="
Cancel-Lock: sha1:PuGv/BM5eIOmiD6+oUAzFXzjVZk=
X-Priority: 3
X-MSMail-Priority: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.7246
X-Newsreader: Microsoft Outlook Express 6.00.2900.5931
X-RFC2646: Format=Flowed; Response
 by: Randy Brukardt - Tue, 26 Sep 2023 05:53 UTC

"J-P. Rosen" <rosen@adalog.fr> wrote in message
news:uem2id$moia$1@dont-email.me...
> Le 22/09/2023 � 22:05, Jeffrey R.Carter a �crit :
>> On 2023-09-22 21:30, Blady wrote:
>>>
>>> A.10.7 Input-Output of Characters and Strings
>>> For an item of type Character the following procedures are provided:
>>> procedure Get(File : in File_Type; Item : out Character);
>>> procedure Get(Item : out Character);
>>> After skipping any line terminators and any page terminators, reads the
>>> next character from the specified input file and returns the value of
>>> this character in the out parameter Item.
>>> The exception End_Error is propagated if an attempt is made to skip a
>>> file terminator.
>>
>> As you have quoted, Get (Character) skips line terminators. End_Of_File
>> returns True if there is a single line terminator before the file
>> terminator, but False if there are multiple line terminators before the
>> file terminator. So you either have to explicitly skip line terminators,
>> or handle End_Error.
>>
> And this works only if the input file is "well formed", i.e. if it has
> line terminators as the compiler expects them to be (f.e., you will be in
> trouble if the last line has no LF).
> That's why I never check End_Of_File, but handle the End_Error exception.
> It always works.

Agreed. And if the file might contain a page terminator, things get even
worse because you would have to mess around with End_of_Page in order to
avoid hitting a combination that still will raise End_Error. It's not worth
the mental energy to avoid it, especially in a program that will be used by
others. (I've sometimes used the simplest possible way to writing a
"quick&dirty" program for my own use; for such programs I skip the error
handling as I figure I can figure out what I did wrong by looking at the
exception raised. But that's often a bad idea even in that case as such
programs have a tendency to get reused years later and then the intended
usage often isn't clear.)

Randy.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor