这是本[文章]的随访阿达:读取文件时添加例外在一个单独的程序
当我下面的代码打开了一个不存在的,而不是投掷,文件ADA.IO_EXCEPTIONS.NAME_ERROR
,它抛出一个ADA.IO_EXCEPTIONS.STATUS_ERROR
。
下面是代码。
主文件: 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 out Ada.Text_IO.File_Type; Name : in String) is separate;
begin
Open_Data (File => Input_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;
而separate
过程体test_read-open_data.adb
:
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;
时引发的错误信息 :
****File not found....****
Execution terminated by unhandled exception
Exception name: ADA.IO_EXCEPTIONS.STATUS_ERROR
Message: file not open
Call stack traceback locations:
0x40bf0f 0x40ead0 0x424b08 0x424e4a 0x4010b4 0x401146 0x7c817075
[2012-03-21 13:45:44] process exited with status 1 (elapsed time: 00.13s)
我已经尝试了几件事情:
把
with Ada.IO_Exceptions;
在test_read.adb
和放when Ada.IO_Exceptions.Name_Error =>
代替when Ada.Text_IO.Name_Error =>
在test_read-open_data.adb
。 但没有改变。在
test_read-open_data.adb
,我改变了线Mode => Ada.Text_IO.In_File
使它成为Mode => Ada.Text_IO.Out_File
,如下所示:Ada.Text_IO.Open(文件=>文件,模式=> Ada.Text_IO.Out_File,名称=>名称);
但是,有没有改善。
因此,如何得到正确的ADA.IO_EXCEPTIONS.NAME_ERROR
?
一个相关的问题:
什么之间的区别Ada.Text_IO.Name_Error
和ADA.IO_EXCEPTIONS.NAME_ERROR
? 在我的节目,我想被告知,当名的文件fx.txt
不存在。 这两个例外的哪一个比较合适?
非常感谢
UPDATE(最新的代码)
这里是我的更新代码:
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;
Success : Boolean;
procedure Open_Data (File : in out Ada.Text_IO.File_Type; Name : in String; Success : out Boolean) is separate;
begin
Open_Data (File => Input_File, Name => "fx.txt", Success => Success);
if not Success then
return;
end if;
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);
Ada.Text_IO.Put_Line (Item => "Reading file success: " & Boolean'Image (Success));
end Test_Read;
和separate
步骤:
separate (test_read)
procedure Open_Data (File : in out Ada.Text_IO.File_Type;
Name : in String; Success : out Boolean) is
--this procedure prepares a file for reading
begin
Success := True;
begin
Ada.Text_IO.Open
(File => File,
Mode => Ada.Text_IO.Out_File,
Name => Name);
exception
when Ada.Text_IO.Name_Error =>
Success := False;
Ada.Text_IO.Put (File => Standard_Error, Item => "****File not found....****");
end;
end Open_Data;
与文件fx.txt目前编译,我得到:
Execution terminated by unhandled exception
Exception name: ADA.IO_EXCEPTIONS.MODE_ERROR
Message: file not readable
Call stack traceback locations:
0x40bf2b 0x40ead0 0x424b58 0x424f5a 0x4010b4 0x401146 0x7c817075
[2012-03-22 08:39:39] process exited with status 1 (elapsed time: 00.14s)
而文件的内容fx.txt
**将被删除 ,其内容未在运行程序时所显示**
与更名为fy.txt文件fx.txt编译,我得到:
****File not found....****
[2012-03-22 08:45:31] process terminated successfully (elapsed time: 00.13s)
那么,什么地方出了错?