艾达(Ada):在block语句中写入文件 [英] Ada: Writing to files within a block statement
问题描述
我正在处理一个数组,数组的长度是在程序执行期间确定的。因此,我使用了 block
语句,可以在其中设置数组的限制。
I am dealing with an array whose length is determined during the execution of the program. So I am making use of a block
statement in which I can set the limits of the array.
我在使用存根进行写过程时遇到了将数组元素写到文件中的问题。我删除了存根以使所有内容都在同一代码中。尽管现在我的代码可以编译并运行,但它没有写入文件。这是代码:
I am having problems to write the elements of the array to a file as I was using a stub for the write procedure. I removed the stub to have everything in the same code. Though now my code compiles and run, it is not writing to the file. Here is the code:
with Ada.Float_Text_IO;
with Ada.Integer_Text_IO;
with Ada.Text_IO;
procedure Compute_Parameters is
Spin_Speed, Whirling_Speed : Float;
Time_Step, Rotor_Revolutions : Float;
Number_Of_Steps : Float;
begin
Ada.Text_IO.Put("Enter the spin speed ");
Ada.Float_Text_IO.Get (Item => Spin_Speed);
Ada.Text_IO.New_Line (1);
Ada.Text_IO.Put("Enter the whirling speed ");
Ada.Float_Text_IO.Get (Item => Whirling_Speed);
Ada.Text_IO.New_Line (1);
Ada.Text_IO.Put("Enter the time step ");
Ada.Float_Text_IO.Get (Item => Time_Step);
Ada.Text_IO.New_Line (1);
Ada.Text_IO.Put("Enter the number of revolutions of the rotor ");
Ada.Float_Text_IO.Get (Item => Rotor_Revolutions);
Number_Of_Steps := (360.0 / (Time_Step * Whirling_Speed)) * Rotor_Revolutions * (Whirling_Speed / Spin_Speed);
declare
type Vector is array (Integer range <>) of Float;
Time_Vector : Vector (1 .. Integer (Float'Truncation (Number_Of_Steps)) + 1);
Rotor_Position_Degrees : Vector (1 .. Integer (Float'Truncation (Number_Of_Steps)) + 1);
Count : Integer := 0;
Start : Float := 0.0;
Step : Float := Time_Step;
Output_Data_File : File_Type;
procedure Write_Files (Output_File : File_Type;
Out_1 : Integer;
Out_2 : Float;
Prec : Natural := 5
) is
begin
Ada.Integer_Text_IO.Put (File => Output_File, Item => Out_1);
Ada.Text_IO.Put (Output_File, " ");
Ada.Float_Text_IO.Put (File => Output_File, Item => Out_2, Fore => 6, Aft => Prec, Exp => 0);
Ada.Text_IO.New_Line (Output_File);
end Write_Files;
begin -- begin of Declare
Ada.Text_IO.Put ("Put file name to write: ");
Create (Output_Data_File, Out_File, Get_Line);
for I in 1 .. Time_Vector'Length loop
Count := Count + 1;
Time_Vector(I) := Start + Step * Float(I-1);
Put (Integer'Image(Count));
Ada.Text_IO.Put(" ");
Rotor_Position_Degrees(I) := Spin_Speed * Time_Step * Float(I-1);
Ada.Float_Text_IO.Put (Item => Rotor_Position_Degrees(I), Fore => 5, Aft => 1, Exp => 0);
Ada.Text_IO.New_Line(1);
--write to file
Write_Files (Output_Data_File,
Out_1 => Count,
Out_2 => Rotor_Position_Degrees(I)
);
end loop;
close(Output_Data_File);
end; -- end of Declare
end Compute_Parameters;
我注意到之后的两行声明
中的>根本没有执行:
I notice that the 2 lines just after begin
in Declare
are not being executed at all:
Ada.Text_IO.Put ("Put file name to write: ");
Create (Output_Data_File, Out_File, Get_Line);
我在做什么错了?
谢谢...
推荐答案
在最后一个获取
之后,按回车键 Rotor_Revolutions
在标准输入中留空行,有待阅读:
Pressing return after your last Get
for Rotor_Revolutions
has left an empty line in standard input, which remains to be read:
Ada.Text_IO.Put_Line (Ada.Text_IO.Get_Line);
Ada.Text_IO.Put ("Put file name to write: ");
Create (Output_Data_File, Out_File, Ada.Text_IO.Get_Line);
只需澄清一下:这是 Get_Line
需要; Put_Line
只是为了表明它是空行。
Just to clarify: It's the Get_Line
that's required; the Put_Line
is just to show that it's an empty line.
或者,使用 Ada.Command_Line
,如此示例所示。
这篇关于艾达(Ada):在block语句中写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!