From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c22608a39b6f5e7b X-Google-Attributes: gid103376,public From: jerry@jvdsys.stuyts.nl Subject: Re: Dynamic Array Sizing Date: 1999/06/19 Message-ID: #1/1 X-Deja-AN: 491395476 Sender: jerry@stuyts.nl (Jerry van Dijk) References: <376B1811.666F042@hotmail.com> Organization: * JerryWare HQ *, Leiden, Holland User-Agent: tin/pre-1.4-980226 (UNIX) (Linux/2.2.9 (i586)) Newsgroups: comp.lang.ada Date: 1999-06-19T00:00:00+00:00 List-Id: Nick Wilson wrote: : I have a file containing an variable amount of data that I want to read : into an array. My problem is that if I use a dynamic array, eg : type Array_Store is array (integer range <>) of boolean; : I have to declare an actual array sometime before reading the values : into the array, but don't know what size to make the array. I can do two : passes of the file, one to see the size, then declare the array in a : block and do another pass to read the values into it. If you are using GNAT, you could do something like: -- * notes: - assumes 1-bit booleans -- - using streams is slower with GNAT.OS_Lib; use GNAT.OS_Lib; with Ada.Text_IO; use Ada.Text_IO; with Ada.Sequential_IO; with Ada.Unchecked_Deallocation; procedure Stuff is Data_Size_Error : exception; -- no data in file File_Name_Error : exception; -- file cannot be opened type Data_Storage_Type is array (Positive range <>) of Boolean; -- an array to store the data in type Data_Access is access all Data_Storage_Type; -- a pointer to the data procedure Free is new Ada.Unchecked_Deallocation (Data_Storage_Type, Data_Access); -- deallocate a data storage array package Boolean_File is new Ada.Sequential_IO (Boolean); use Boolean_File; -- package for using files of Booleans Data : Data_Access; Num_Elements : Positive; File_Name : constant String := "test.dat"; ----------------------------------- -- Get number of data elements -- ----------------------------------- function Data_Elements (Path : String) return Positive is Fd : File_Descriptor; Size : Long_Integer; C_Name : String := Path & ASCII.NUL; begin Fd := Open_Read (C_Name'Address, Binary); if Fd = Invalid_FD then raise File_Name_Error; end if; Size := File_Length (Fd); Close (Fd); if Size < 1 then raise Data_Size_Error; end if; return Positive (Size); end Data_Elements; ------------------------------------------------------------ -- Create data storage array and return a pointer to it -- ------------------------------------------------------------ function Create_Data_Storage (Elements : Positive) return Data_Access is Storage : Data_Access; begin if Elements < 1 then raise Data_Size_Error; end if; Storage := new Data_Storage_Type (1 .. Elements); return Storage; end Create_Data_Storage; --------------------------------------- -- Read data in data storage array -- --------------------------------------- procedure Load_Data (Path : in String; Data : in Data_Access) is File : Boolean_File.File_Type; begin Open (File, In_File, Path); for I in 1 .. Data.all'Length loop Read (File, Data(I)); end loop; Close (File); end Load_Data; begin Num_Elements := Data_Elements (File_Name); Data := Create_Data_Storage (Num_Elements); Load_Data (File_Name, Data); Put_Line ("Loaded" & Data.all'Length'Img & " Booleans from " & File_Name); Free (Data); end Stuff; -- -- Jerry van Dijk | Leiden, Holland -- Team Ada | jdijk@acm.org -- see http://stad.dsl.nl/~jvandyk