comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <jrcarter@acm.org>
Subject: Re: Text_IO.End_Of_File Problem
Date: Mon, 26 Nov 2001 01:53:58 GMT
Date: 2001-11-26T01:53:58+00:00	[thread overview]
Message-ID: <3C01A0AA.AF2F54BF@acm.org> (raw)
In-Reply-To: fb75c450.0111251442.3e88ff5f@posting.google.com

Hambut wrote:
> 
> Hi,
> 
> I'm getting an exception with the attached code, and I can't see what
> I'm doing wrong, or why it's falling over.

It's not falling over, it's terminating with an unhandled exception.

> The output I get from executing the code is:
> 
> "
>  13
>  13
> 
> raised ADA.IO_EXCEPTIONS.END_ERROR : a-textio.adb:394
> "
> 
> =====Code Follows======
> 
> with Text_Io;
> with Ada.Sequential_Io;
> procedure Eof_Fails is
> 
>    package Io is new Ada.Sequential_Io( Character );
> 
>    Test_File_Name : constant String := "Test_fails";
>    Sequential_File : Io.File_Type;
>    Text_File : Text_Io.File_Type;
> 
>    Failure_Characters : constant String := ( 1 => Ascii.CR,
>                                              2 => Ascii.CR,
>                                              3 => Ascii.LF,
>                                              4 => Ascii.CR,
>                                              5 => Ascii.CR,
>                                              6 => Ascii.LF,
>                                              7 => Ascii.CR,
>                                              8 => Ascii.LF );

What you are doing is writing characters that contain embedded within
them what your specific system considers line terminators. Based on your
sample output, I would guess you're running under Win32. On such
systems, a line terminator is a CR followed by an LF. So you have (in
Text_IO terms) 3 lines. The first 2 lines contain a single character,
which is a CR; the 3rd line is null.

Next, note how Ada.Text_IO.Get (Character) works. It skips any line
terminators looking for a character. So your first call to Get reads
item 1 above. The second skips a line terminator (items 2 & 3) and reads
item 4. You are not now at the end of the file, so End_Of_File returns
False. Your 3rd call to get skips 2 line terminators (items 5-8), which
brings you to the end of the file, raising End_Error.

Note that on a different system, with (a) different character(s)
representing a line terminator, you would get different results, but you
could still define a value for Failure_Characters that would cause the
program to raise End_Error even though End_Of_File is False.

> 
> -- These seem to work OK.
> --       Failure_Characters : constant String := ( 1 => Ascii.CR,
> --                                                 2 => Ascii.CR,
> --                                                 3 => Ascii.LF,
> --                                                 4 => Ascii.CR );

This would work on Win32 because after the 2nd call to Get (which reads
item 4), you are at the end of the file, so End_Of_File returns True.

> 
>    C : Character;
> begin
>    -- First set up the test file
>    --
>    Io.Create( File => Sequential_File,
>               Name => Test_File_Name );
> 
>    for I in Failure_Characters'First..Failure_Characters'Last loop

Note that X'First .. X'Last is equivalent to X'range, which is clearer.

>       Io.Write( File => Sequential_File,
>                 Item => Failure_Characters(I) );
>    end loop;
> 
>    Io.Close( File => Sequential_File );
> 
>    -- Now try and read in as a text file
>    --
>    Text_Io.Open( File => Text_File,
>                  Mode => Text_Io.In_File,
>                  Name => Test_File_Name );
> 
>    while not Text_Io.End_Of_File( File => Text_File ) loop
>       Text_Io.Get( File => Text_File,
>                    Item => C );
>       Text_Io.Put_Line( Integer'Image( Character'Pos(C)));
>     end loop;
> 
>    Text_Io.Close( File => Text_File );
> exception
>    when others =>
>       Text_Io.Close( File => Text_File );
>       raise;
> end Eof_fails;

The basic lesson is that Text_IO interprets the characters in your file,
and in the process does not return every character to you. This can
result in some operations reading past the end of the file, although
End_Of_File has just returned False.

-- 
Jeff Carter
"We call your door-opening request a silly thing."
Monty Python & the Holy Grail



  reply	other threads:[~2001-11-26  1:53 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-25 22:42 Text_IO.End_Of_File Problem Hambut
2001-11-26  1:53 ` Jeffrey Carter [this message]
2001-11-26 12:30   ` Hambut
2001-11-26 16:50     ` Mark Biggar
2001-11-26 13:25   ` Hambut
2001-11-26  2:15 ` Patrick Hohmeyer
2001-11-26 12:36   ` Hambut
2001-11-26  2:35 ` Nick Roberts
2001-11-26 12:13   ` Hambut
2001-11-26 18:00     ` Nick Roberts
2001-11-27  9:51       ` Hambut
replies disabled

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