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,5997b4b7b514f689 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Reading a line of arbitrary length Date: 1997/02/16 Message-ID: #1/1 X-Deja-AN: 219205379 References: <5ds40o$rpo@fg70.rz.uni-karlsruhe.de> Content-Type: text/plain; charset=ISO-8859-1 Organization: Estormza Software Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1997-02-16T00:00:00+00:00 List-Id: In article <5ds40o$rpo@fg70.rz.uni-karlsruhe.de>, Thomas.Koenig@ciw.uni-karlsruhe.de wrote: >What's the best way of reading a line of arbitrary length in Ada? > >Get_Line only works on Strings, not Unbounded_Strings. Would I need >to read in chunks of fixed length, then paste them together? Yes. There is a very simple way to do this that does not require explicit heap allocation nor GNAT-specific extensions to the language. When you call Get_Line, e.g. declare The_Buffer (1 .. 80); Last : Natural; begin Ada.Text_IO.Get_Line (The_Buffer, Last); end; then Last will be in the range [0, The_Buffer'Last]. The key is realizing that if Last = The_Buffer'Last after the call, then there's more data on the same line (even if it's just the end of line marker). You just have to keep reading until Last < The_Buffer'Last. Here's the code to do it: with Ada.Strings.Unbounded; procedure Get_Line ( Item : in out Ada.Strings.Unbounded.Unbounded_String; Buffer_Length : in Positive := 80); with Ada.Text_IO; procedure Get_Line ( Item : in out Ada.Strings.Unbounded.Unbounded_String; Buffer_Length : in Positive := 80) is The_Buffer : String (1 .. Buffer_Length); Last : Natural; use Ada.Strings.Unbounded; begin -- Get_Line Item := Null_Unbounded_String; loop Ada.Text_IO.Get_Line (The_Buffer, Last); Append (Item, New_Item => The_Buffer (1 .. Last)); exit when Last < The_Buffer'Last; end loop; end Get_Line; -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271