在Ada中打开,写入和追加的过程 [英] Procedure to open, write and append in Ada
问题描述
这个问题是后续的帖子在
关于在Ada中写入文件
我选择将这个问题放在一个单独的帖子中,以便更多人可以看到这个问题,因为我已经接受了一个稍有不同的问题(这是文件处理中的例外情况) 。
WITH Ada.Sequential_IO;
与Ada.Float_Text_IO;
PROCEDURE TEST是
包Seq_Float_IO是新的Ada.Sequential_IO(Element_Type => Float);
X_File:Seq_Float_IO.File_Type;
File_Name:String;
过程Open_Data(File:in out Seq_Float_IO.File_Type;
Name:in String)是
BEGIN
开始
Seq_Float_IO.Open(
File =>文件,
Mode => Seq_Float_IO.Append_File,
Name => File_Name);
例外
当Seq_Float_IO.Name_Error =>
Seq_Float_IO.Create(
File => File,
Mode => Seq_Float_IO.Out_File,
Name => File_Name);
end;
END Open_Data;
x:CONSTANT Float:= 2.0;
BEGIN - 主程序
Open_Data(X_File,xvalues.dat);
Seq_Float_IO.Write(File => X_File,Item => x);
Seq_Float_IO.Close(File => X_File);
结束测试;
在编译上面的代码时,我得到如下错误:
- X_File:Seq_Float_IO.File_Type;
- File_Name:String;
|
$ b
不受约束的子类型不允许(需要初始化)
提供初始值或显式数组
- File_Name:String;
我不知道2件事:
- 我有File_Name:String;因为我希望能够写入不同的文件。所以我想要一个通用的字符串,而不是像这样:
File_Name:CONSTANT String:=one_File_Only.dat
- 将程序Open_Data保存在单独的广告和adb(用于正文)文件中会更好吗?
非常感谢...
全新。
我修改了代码如下:
与Ada.Sequential_IO;
PROCEDURE TEST1是
包Seq_Float_IO是新的Ada.Sequential_IO(Element_Type => Float);
X_File,Y_File:Seq_Float_IO.File_Type;
Name_X:CONSTANT String:=domainvalues.dat;
Name_Y:CONSTANT String:=ordinatevalues.dat;
过程Open_Data(File:in out Seq_Float_IO.File_Type; Name:in String)是
BEGIN
begin
Seq_Float_IO.Open(
File => File,
Mode => Seq_Float_IO.Append_File,
Name => Name_X);
例外
当Seq_Float_IO.Name_Error =>
Seq_Float_IO.Create(
File => File,
Mode => Seq_Float_IO.Out_File,
Name => Name_X);
end;
END Open_Data;
x:CONSTANT Float:= 2.0;
BEGIN --main program
Open_Data(File => X_File,Name =&Name_X);
Seq_Float_IO.Write(File => X_File,Item => x);
Seq_Float_IO.Close(File => X_File);
Open_Data(File => Y_File,Name => Name_Y);
Seq_Float_IO.Write(File => Y_File,Item => x);
Seq_Float_IO.Close(File => Y_File);
END TEST1;
正如你所看到的那样
<$ p $ (
File =>文件,
Mode => Seq_Float_IO.Append_File,
Name => Name_X); p>Seq_Float_IO.Open
我已经把Name_X作为Name的参数,但这是不对的,因为我应该可以传入一个可以是Name_X或Name_Y的通用名称。抱歉的家伙,我不知道该把什么放在这里。
我非常感谢你的帮助。谢谢
解决方案在Ada中,普通的
String
字符串,如File_Name
,必须是固定长度的;但是不同的字符串可以有不同的长度。
您可以写
S1:String:=1234;
S2:String:=12345;
在这种情况下,S1的长度是4,分配的长度必须是4。可写
S1:=abcd;
但如果您尝试写入
S1:=pqrst;
或
S1:= S2;
你会得到一个
Constraint_Error
p>
对于子程序的字符串参数(例如
Open_Data
),String参数Name
需要长度 - 当然还有值!呼叫中的实际参数。所以你可以说:
$ b $ pre $Open_Data(X_File,x.dat);
Open_Data(Y_File,a_very_long_name.dat);
您之前遇到过问题
<$ p (File:in FileSeq_Float_IO.File_Type;
Name:in String)是
begin
Seq_Float_IO.Open(File => File,
Mode => Seq_Float_IO.Append_File,
Name => ????);
我不愿意告诉你答案,所以考虑
File =>文件
部分。第一个File
是Seq_Float_IO.Open
的形式参数的名字,第二个File
是什么被传递,在这种情况下Open_Data
的File
参数。 / p>
如果我指出我可以将上面的调用写成
Open_Data(File => X_File,Name =>x.dat);
Open_Data(File => Y_File,Name =>a_very_long_name.dat);
This question is a follow-up of the post at
Ada file operation: instantiation and exception
about writing to files in Ada.
I chose to place this question in a separate post so that it'll become visible to more people as I already accepted an answer on a slightly different issue (which was on exceptions in file handling) in that aforementioned post.
WITH Ada.Sequential_IO; WITH Ada.Float_Text_IO; PROCEDURE TEST is package Seq_Float_IO is new Ada.Sequential_IO (Element_Type => Float); X_File : Seq_Float_IO.File_Type; File_Name : String; procedure Open_Data(File : in out Seq_Float_IO.File_Type; Name : in String) is BEGIN begin Seq_Float_IO.Open ( File => File, Mode => Seq_Float_IO.Append_File, Name => File_Name ); exception when Seq_Float_IO.Name_Error => Seq_Float_IO.Create ( File => File, Mode => Seq_Float_IO.Out_File, Name => File_Name); end; END Open_Data; x : CONSTANT Float := 2.0; BEGIN --main program Open_Data(X_File, "xvalues.dat"); Seq_Float_IO.Write(File => X_File,Item => x); Seq_Float_IO.Close(File => X_File); END TEST;
On compiling the above I get an error as follows:
- X_File : Seq_Float_IO.File_Type;
- File_Name : String;
|
unconstrained subtype not allowed (need initialization) provide initial value or explicit array bounds
- File_Name : String;
|
I don't know 2 things:
- I have File_Name : String; as I want to be able to write to different files. So I want a general string and not something like:
File_Name : CONSTANT String := "one_File_Only.dat"
- Would it be better to save the procedure Open_Data in separate ads and adb (for the body) files?
Thanks a lot...
NEW...
I've modified the code as follows:
WITH Ada.Sequential_IO; PROCEDURE TEST1 is package Seq_Float_IO is new Ada.Sequential_IO (Element_Type => Float); X_File, Y_File : Seq_Float_IO.File_Type; Name_X : CONSTANT String := "domainvalues.dat"; Name_Y : CONSTANT String := "ordinatevalues.dat"; procedure Open_Data(File : in out Seq_Float_IO.File_Type; Name : in String) is BEGIN begin Seq_Float_IO.Open ( File => File, Mode => Seq_Float_IO.Append_File, Name => Name_X ); exception when Seq_Float_IO.Name_Error => Seq_Float_IO.Create ( File => File, Mode => Seq_Float_IO.Out_File, Name => Name_X); end; END Open_Data; x : CONSTANT Float := 2.0; BEGIN --main program Open_Data(File => X_File, Name => Name_X); Seq_Float_IO.Write(File => X_File, Item => x); Seq_Float_IO.Close(File => X_File); Open_Data(File => Y_File, Name => Name_Y); Seq_Float_IO.Write(File => Y_File, Item => x); Seq_Float_IO.Close(File => Y_File); END TEST1;
As you see I have
Seq_Float_IO.Open ( File => File, Mode => Seq_Float_IO.Append_File, Name => Name_X );
I have put Name_X as the parameter that Name is taking but this is not right as I should be able to pass in a general name which can be either Name_X or Name_Y. Sorry guys, I can't figure out what to put here.
I would much appreciate your help. Thanks
解决方案The thing about plain
String
in Ada is that a particular string, like yourFile_Name
, has to be fixed-length; but different Strings can be of different length.You can write
S1 : String := "1234"; S2 : String := "12345";
in which case S1 is of length 4, and assignments to it have to be of length 4. You can write
S1 := "abcd";
but if you try to write
S1 := "pqrst";
or
S1 := S2;
you will get a
Constraint_Error
.In the case of String parameters to subprograms, like your
Open_Data
, the String parameterName
takes on the length -- and of course the value! of the actual parameter in the call. So you can sayOpen_Data (X_File, "x.dat"); Open_Data (Y_File, "a_very_long_name.dat");
You were having problems earlier with
procedure Open_Data(File : in out Seq_Float_IO.File_Type; Name : in String) is begin Seq_Float_IO.Open (File => File, Mode => Seq_Float_IO.Append_File, Name => ????);
I'm reluctant to just tell you the answer, so -- consider the
File => File
part. The firstFile
is the name of the formal parameter ofSeq_Float_IO.Open
and the secondFile
is what is to be passed, in this caseOpen_Data
'sFile
parameter.It might help if I point out that I could have written the calls above as
Open_Data (File => X_File, Name => "x.dat"); Open_Data (File => Y_File, Name => "a_very_long_name.dat");
这篇关于在Ada中打开,写入和追加的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文 - X_File : Seq_Float_IO.File_Type;