Ada: Adding an exception in a separate procedure w

2019-09-01 00:31发布

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  out 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...

Update:

I have now made the following corrections.

  1. In test_read.adb, I now have procedure Open_Data (File : in out Ada.Text_IO.File_Type; Name : in String) is separate;

  2. Updated the definition of the same Open_Data procedure in test_read-open_data.adb.

The program compiles well though I do not see it catch the exception say if I renamed the file fx.txt to fy.txt. The error message I get is just

raised ADA.IO_EXCEPTIONS.NAME_ERROR : fx.txt: No such file or directory

So I do not get my own error message :File not found.

What is still wrong?

4条回答
SAY GOODBYE
2楼-- · 2019-09-01 00:52

if your goal is to simply check if the file exists, consider using Ada.Directories.Exists

查看更多
SAY GOODBYE
3楼-- · 2019-09-01 00:52

IIRC: Standard_Error is not a file, but a Stream.

查看更多
SAY GOODBYE
4楼-- · 2019-09-01 00:56

The File parameter of Open_Data needs to be an in out parameter (as in, for example, Ada.Text_IO.Create), because you want the opened file to be accessible within Test_Read.

You are getting actual for "File" must be a variable because an in parameter is read-only.

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

(Personally I rarely type the in mode, because it’s the default).

But in any case, it looks as though the reason for the observed behaviour is that Test_Read doesn’t actually call Open_Data!

(edited to make the recommended mode in out & to suggest calling Open_Data)

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-09-01 01:14

I suspect that the reason you are not seeing your error message is that you are using Put rather than Put_Line. Different implementations/platforms treat output to the user's display differently. To be extra sure you will see the message, follow the Put_Line with a Get_Line. The Get_Line generally forces the output of the Put_Line.

查看更多
登录 后发表回答