Ada:在块语句中写入文件

4

我正在处理一个数组,其长度是在程序执行期间确定的。因此,我正在使用一个block语句来设置数组的限制。

我遇到了写入数组元素到文件的问题,因为我使用的是一个写入程序的桩程序。我删除了桩程序,使所有代码都在同一个代码中。尽管现在我的代码已经编译和运行了,但它没有写入文件。以下是代码:

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;

我注意到在Declarebegin后的两行代码根本没有被执行:

Ada.Text_IO.Put ("Put file name to write: ");
Create (Output_Data_File, Out_File, Get_Line);

我做错了什么?
谢谢...

为什么要写 use Ada.Text_IO;,但后面又使用完整的包名 Ada.Text_IO.Put("Enter the spin speed "); 呢?这没有多少意义。请在两者中选择一个。如果您想要使用完全限定名称,则不需要使用 use 部分。 - Robert H
@ Robert 是的,我知道。是我的疏忽。谢谢你告诉我。帖子已经编辑过了。 - yCalleecharan
1个回答

2

在你最后一次获取 Rotor_Revolutions 后按下回车键会留下一个空行在标准输入中,这个空行仍然需要被读取:

 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仅用于显示空行。

或者,可以使用Ada.Command_Line,如此示例所示。


我认为declare块并不相关,除了将源和效果分开。 - trashgod
@trashgod。哇。非常感谢你关于Put_Line的提示。1赞 - yCalleecharan
@trashgod 如果不使用declare块,如何在计算过程中获取数组长度的限制? - yCalleecharan
好的,我需要稍微看一下 Put_LineGet_Line - yCalleecharan
一个 declare 块是进入嵌套作用域的一种完全有效的方式。在这个例子中,我可能忽略了某些东西,但我根本不觉得需要 Vector;输出是逐行构建的。 - trashgod
1
@trashgod 是的,当然。你说得对。由于我直接将内容写入文件中,因此不需要任何“向量”。感谢您的提醒。 - yCalleecharan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接