comp.lang.ada
 help / color / mirror / Atom feed
* Ada.Text_IO.Get_Line issue in windows
@ 2015-06-01  5:07 tornenvi
  2015-06-02  2:44 ` AdaMagica
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: tornenvi @ 2015-06-01  5:07 UTC (permalink / raw)


Hello,

   I have found some unexpected behavior for calls to Ada.Text_IO.Get_Line
   on windows when reading from a text that has only one line in it.

   The program below simply reads the first line of a text file into a 6
   character buffer, and outputs the length of the line read, the line and
   the line again as hex.

   I use windows notepad to create the test file Test.txt.

   When using Test.txt with the one line of text
    ABCD
   with no blank line at the end of the file I get output 1 below

-- Output 1 ------------------------------------------------------
Length=  4
Line=[ABCDXX]
Line as hex=[414243445858]
------------------------------------------------------------------

   as expected. However if Test.txt is the one line of text
    ABCDE
   with no blank line at the end of the file then I get output 2 below

-- Output 2 -----------------------------------------------------
Length=  6
Line=[ABCDE ]
Line as hex=[4142434445ff]
-----------------------------------------------------------------

   I receive an unexpected length of 6, expected 5. The fact that the last two
   characters are not 'XX' is acceptable since ARM A.10.7 19 says
   "The values of characters not assigned are not specified."

   I get the same output 2 if I compile the program with
   GNAT GPL 2014, 2013,2012 or 2011 or with mingw FSF GNAT 4.7.0-1 or 4.8.1-4.

   However if I compile with mingw FSF GNAT 4.5.2-1 , 4.5.0-1, 4.4.0 or
   3.4.5 then I get the expected output 3 below

-- Output 3 -----------------------------------------------------
Length=  5
Line=[ABCDEX]
Line as hex=[414243444558]
-----------------------------------------------------------------

   Under windows the one line text file does not have an end of line
   character (usually CRLF) which means it is not a canonical text file
   as described here

   http://docs.adacore.com/gnat_rm-docs/html/gnat_rm/gnat_rm/the_implementation_of_standard_i_o.html#text-io

   however that section also states that text_io can be use to read non canonical text files
   and the very last line of that section says
   "Every LF character is considered to end a line, and there is an
     implied LF character at the end of the file."

   this indicates to me that there should be an implied LF at the end of the
   file and I should be getting the expected output 3 result for
   all the compilers.

   Is this a bug in the windows builds of GNAT?





---- Program -----------------------------------------------------
with Ada.Text_IO;

procedure FileRead is


   TestFileName : constant string := "Test.txt";
   TestFile : Ada.Text_IO.File_Type;

   Line   : string(1..6) := "XXXXXX";
   Length : natural;

   function ToHex(Msg : string) return string is
     Hex   : constant array (0..15) of character := "0123456789abcdef";
     Str   : String(1..Msg'length*2) := (others => ' '); -- 2 hex digits per byte
     Index : Integer :=1;
     ByteValue : Integer;
   begin
     for i in Msg'Range loop
       ByteValue := character'Pos(Msg(i));
       Str(Index) := Hex(ByteValue / 16);
       Index := Index + 1;
       Str(Index) := Hex(ByteValue mod 16);
       Index := Index + 1;
     end loop;
     return Str;
   end;

begin

   Ada.Text_IO.Open( TestFile, Ada.Text_IO.IN_FILE, TestFileName );
   if not Ada.Text_IO.End_Of_File(TestFile) then
     Ada.Text_IO.Get_Line( TestFile, Line, Length );
     Ada.Text_IO.Put_Line("Length= " & integer'image(Length));
     Ada.Text_IO.Put_Line("Line=[" & Line & "]");
     Ada.Text_IO.Put_Line("Line as hex=[" & ToHex(Line) & "]");
   end if;

exception

   when E: others =>
     Ada.Text_IO.Put_Line("Exception");

end;
-------------------------------------------------------------





^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2015-06-08 22:47 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-01  5:07 Ada.Text_IO.Get_Line issue in windows tornenvi
2015-06-02  2:44 ` AdaMagica
2015-06-03  5:54   ` tornenvi
2015-06-03  6:32     ` Jeffrey R. Carter
2015-06-05  4:10       ` tornenvi
2015-06-03  7:02 ` Simon Wright
2015-06-03 10:08   ` Simon Clubley
2015-06-03 11:14     ` Simon Wright
2015-06-08 22:47 ` wowwomenonwheels205

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