1

Hi im just wondering how to put data in an array if i loop txt and store it in A_Composite Name.

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;
Simon Wright
  • 25,108
  • 2
  • 35
  • 62
  • please add sample input and explain what exactly you expect, what happens instead, and if there are any, give error messages. – hoijui Jul 14 '15 at 15:16
  • possible duplicate of [Ada Array store unbounded String getting from .txt file](http://stackoverflow.com/questions/31386314/ada-array-store-unbounded-string-getting-from-txt-file) – Simon Wright Jul 14 '15 at 15:33
  • 1
    Are you sure you want to store the lines in an array? Why do you want to store them in an array? Are you aware that array objects can't change their range? – Jacob Sparre Andersen Jul 15 '15 at 07:23
  • 1
    Have you tried to compile the source text you have posted? – Jacob Sparre Andersen Jul 15 '15 at 07:25

1 Answers1

0

Ok, you have an unconstrained array here. This has implications; you see an unconstrained array gains its definite length when the object (general sense, not OOP) is declared or initialized.

As an example, let's look at strings (which are unconstrained arrays of characters) for an example to see how this works:

-- 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 := ('*','*','*','*','*','*','*','*','*','*');

Now, you can get these bounds from a function call; this means that we can use function-calls to return these values recursively.

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;

So, that's how you'd do it using an unconstrained array.

Shark8
  • 4,095
  • 1
  • 17
  • 31