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,70dee47014cd7083 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: How do I allocate strings of variable length at runtime? Date: 1997/09/26 Message-ID: #1/1 X-Deja-AN: 275916570 References: <342bbda6.153101@news.rmi.de> Organization: Estormza Software Newsgroups: comp.lang.ada Date: 1997-09-26T00:00:00+00:00 List-Id: In article <342bbda6.153101@news.rmi.de>, hilgend@rmi.de (Oliver Hilgendorf) wrote: >I want to open several textfiles using text_io.open. The length of >each filename is only know at runtime. However if I define a string of >the maximum filename-length to hold the filename, all trailing blanks >are also passed to text_io.open and this results in name_error because >a file with trailing blanks does not exist. >So I have to pass a string that is defined with exactly the same >length as the actual filename. >How can I define a string object with variable length at runtime? You need to specify more information: What is the source of filenames? Do you have to store the filenames prior to opening the files? What you need to do is use a bounded string or an unbounded string. These data structures keep track of the logical length of the filename, even if the physical length of the data structure is larger. Your basic problem is that you're using an array as a data structure directly, when arrays (and similarly linked lists) should only be used to implement more abstract data structures (such as a bounded buffer). A rule of thumb for determining whether you need a better abstraction is, Is all of the array used to store the data, or only part? Is there another piece of data used to keep track of the last index position of the "good" data, after which is unused garbage? If so, then you want to bundle them together as part of another, higher-layer-of-abstraction data structure. In fact, for strings, that data structure has already been written for you: Ada.Strings.Bounded and Ada.Strings.Unbounded. Email me if you have any more questions. Matt -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271