Ada:读取文件时在单独的过程中添加异常 [英] Ada: Adding an exception in a separate procedure when reading a file
问题描述
这是对这个问题的追踪: Ada:从文件读取。
我想添加一个异常
,检查我打开的文件是否实际存在,不。我已经做了一个单独的
程序,以避免代码混乱。
这是主要代码 test_read.adb
:
with Ada.Text_IO;使用Ada.Text_IO;
with Ada.Long_Float_Text_IO;
与Ada.Float_Text_IO;
程序Test_Read是
Input_File:File_Type;
值:Long_Float;
程序Open_Data(File:in Ada.Text_IO.File_Type; Name:in String)是分开的;
开始
Ada.Text_IO.Open(File => Input_File,Mode => Ada.Text_IO.In_File,Name =>fx.txt);
而不是End_Of_File(Input_File)循环
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;
结束循环;
Ada.Text_IO.Close(File => Input_File);
end Test_Read;
这里是单独的
body test_read-open_data.adb
程序 Open_Data
:
separate(test_read)
procedure Open_Data(File:in out Ada.Text_IO.File_Type;
Name:in String)is
--this程序准备一个文件阅读
begin
begin
Ada.Text_IO.Open
(File => File,
Mode => Ada.Text_IO.In_File,
Name => Name);
exception
当Ada.Text_IO.Name_Error =>
Ada.Text_IO.Put(File => Standard_Error,Item =>找不到文件);
结束
结束Open_Data;
在编译时,我在中单独出现错误信息
body test_read-open_data.adb
:
必须是一个变量
如何解决?
非常感谢...
更新:
现在已经进行了以下更正。
-
在
test_read.adb
我现在有程序Open_Data(File:in out Ada.Text_IO.File_Type; Name:in String)是分开的;
<在
test_read-open_data.adb $ c $中更新相同 Open_Data
过程的定义 c>。
程序编译很好,虽然我没有看到它捕获异常说如果我重命名文件 fx.txt
to fy.txt
。我得到的错误信息只是
raise ADA.IO_EXCEPTIONS.NAME_ERROR:fx.txt:没有这样的文件或目录
所以我没有收到我自己的错误信息:找不到文件
>还有什么问题?
Open_Data的
参数需要是一个 in out
参数(例如, Ada.Text_IO.Create
),因为您想要在 Test_Read
中访问打开的文件。
您正在获得实际的文件必须是变量
,因为参数中的参数是只读的。
程序Open_Data(File:in out Ada.Text_IO.File_Type;
模式中键入
Name:in String)是
$ c $ (个人,我很少在,因为它是默认的)。
p>
但无论如何,它看起来好像是原因观察到的行为是 Test_Read
实际上并不调用 Open_Data
!
(编辑为推荐模式输入
&建议调用 Open_Data
)
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.
In
test_read.adb
, I now haveprocedure Open_Data (File : in out Ada.Text_IO.File_Type; Name : in String) is separate;
Updated the definition of the same
Open_Data
procedure intest_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?
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
)
这篇关于Ada:读取文件时在单独的过程中添加异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!