Reading Files with Ada Streams


with Ada.Command_Line;
with Ada.Exceptions;
with Ada.Streams.Stream_IO;
with Ada.Text_IO;
procedure Stream_IO
is
   File : Ada.Streams.Stream_IO.File_Type;
   Stream : Ada.Streams.Stream_IO.Stream_Access;
begin
   if Ada.Command_Line.Argument_Count /= 1 then
      Ada.Text_Io.Put_Line
        (File => Ada.Text_Io.Standard_Error,
         Item => "USAGE: " & Ada.Command_Line.Command_Name &
         " <input-file-name>");
      Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
      return;
   end if;
   declare
      Filename : String renames Ada.Command_Line.Argument (1);
   begin
      Ada.Streams.Stream_IO.Open
        (File => File,
         Name => Filename,
         Mode => Ada.Streams.Stream_IO.In_File);
   exception
      when E: others =>
         Ada.Exceptions.Raise_Exception
           (E => Ada.Exceptions.exception_Identity (E),
            Message =>
              " attempting to open file """ & Filename & """.");
   end;
   Stream := Ada.Streams.Stream_IO.Stream (File);
   while not Ada.Streams.stream_IO.End_Of_File (File) loop
      Ada.Text_Io.Put_Line
        (Ada.Streams.stream_element'Image
         (Ada.Streams.Stream_Element'Input (Stream)));
   end loop;

   Ada.Streams.Stream_IO.Close (File);
end Stream_IO;

Contributed by: David C. Hoos, Sr.
Contributed on: October 13, 1999
License: Public Domain

Back