Ada: Adding an exception in a separate procedure when reading a file

Posted by yCalleecharan on Stack Overflow See other posts from Stack Overflow or by yCalleecharan
Published on 2012-03-20T20:34:00Z Indexed on 2012/03/20 23:29 UTC
Read the original article Hit count: 338

Filed under:
|
|
|

This is a follow-up of this question: Ada: reading from a file .

I would like to add an exception that checks if the file that I'm opening actually exists or not. I have made a separate procedure to avoid code clutter.

Here is the main code test_read.adb:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Long_Float_Text_IO;
with Ada.Float_Text_IO;


procedure Test_Read is

   Input_File    : File_Type;
   Value         : Long_Float;

procedure Open_Data (File : in  Ada.Text_IO.File_Type; Name : in String) is separate;

begin

   Ada.Text_IO.Open (File => Input_File, Mode => Ada.Text_IO.In_File, Name => "fx.txt");

   while not End_Of_File (Input_File) loop
      Ada.Long_Float_Text_IO.Get (File => Input_File, Item => Value);
      Ada.Long_Float_Text_IO.Put (Item => value, Fore => 3, Aft  => 5, Exp  => 0);
      Ada.Text_IO.New_Line;
   end loop;
   Ada.Text_IO.Close (File => Input_File);

end Test_Read;

And here is the separate body test_read-open_data.adb of the procedure Open_Data:

separate(test_read)
procedure Open_Data (File : in  Ada.Text_IO.File_Type; 
                     Name : in String) is

   --this procedure prepares a file for reading
   begin
      begin
       Ada.Text_IO.Open
         (File => File,
          Mode => Ada.Text_IO.In_File,
          Name => Name);
       exception
       when Ada.Text_IO.Name_Error =>
          Ada.Text_IO.Put(File => Standard_Error, Item => "File not found.");
      end;
end Open_Data;

On compilation I get an error message in the separate body test_read-open_data.adb:

actual for "File" must be a variable

How to fix this?

Thanks a lot...

© Stack Overflow or respective owner

Related posts about file

Related posts about exception