comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Weird behavior of Get character with trailing new lines.
Date: Fri, 22 Sep 2023 22:52:21 +0300	[thread overview]
Message-ID: <kn69jlFq9j6U1@mid.individual.net> (raw)
In-Reply-To: <uekq08$clvb$1@dont-email.me>

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;





  reply	other threads:[~2023-09-22 19:52 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 [this message]
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
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