Ada - 从文本文件中获取字符串并存储在数组中

时间:2015-07-14 15:14:15

标签: ada

嗨,我想知道如果我循环txt并将其存储在A_Composite名称中,如何将数据放入数组中。

procedure Main is
       type An_Array is array (Natural range <>) of A_Composite;
       type A_Composite is
          record
             Name  : Unbounded_String;
          end record;

       File       : Ada.Text_IO.File_Type;
       Line_Count : Integer := 0;


    begin
       Ada.Text_IO.Open (File => File,
                         Mode => Ada.Text_IO.In_File,
                         Name => "highscore.txt");

       while not Ada.Text_IO.End_Of_File (File) loop
          declare
                Line :String := Ada.Text_IO.Get_Line (File);            
          begin

             --I want to store Line String to array. but i don't know how to do it      

          end;
       end loop;
       Ada.Text_IO.Close (File);
end Main;

1 个答案:

答案 0 :(得分:0)

好的,你在这里有一个无约束的数组。这有影响;你会看到一个无约束的数组在声明或初始化对象(一般意义,而不是OOP)时获得了它的确定长度。

作为一个例子,让我们看一下字符串( 无约束的字符数组)的示例,看看它是如何工作的:

-- Create a string of 10 asterisks; the initialization takes those bounds.
A : constant string(1..10):= (others => '*');

-- Create a string of 10 asterisks; but determined by the initialization value.
B : constant string := (1..10 => '*');

-- Another way of declaring a string of 10 asterisks.
C : constant string := ('*','*','*','*','*','*','*','*','*','*');

现在,您可以通过函数调用获取这些边界;这意味着我们可以使用函数调用以递归方式返回这些值。

Function Get_Text return An_Array is
  Package Unbounded renames Ada.Strings.Unbounded;
  -- You'll actually want the Get_Line that takes a file.
  Function Get_Line return Unbounded.Unbounded_String
    renames Unbounded.Text_IO.Get_Line;
begin
  return (1 => (Name => Get_Line)) & Get_Text;
exception
  when End_Error => return ( 1..0 => (Name => Unbounded.Null_Unbounded_String) );
end Get_Text;

那么,你是如何使用无约束阵列来做的。