Skip and Read 2
MC logo

Skip and Read 2

Ada Code Examples

<<Operator Overloading Download In and Out Parameters.>>
-- This reads in a string on a line and skips leading blanks, but it uses
-- a procedure.
with Gnat.Io; use Gnat.Io;
procedure ReadRest2 is
   procedure ReadStr(Str: out String; Last: out Integer) is
      I: Integer;               -- Input subscript.
      Ch: Character;            -- Input character.
      -- Read to first non-blank character and put it into the first
      -- position of the array.
         exit when Ch /= ' ';
      end loop;
      Str(Str'first) := Ch;

      -- Read the rest of the line into the part of the array after the
      -- first position.
      Get_Line(Str(Str'First+1..Str'last), Last);

   end ReadStr;

   Max: constant := 20;         -- Max size of string.
   Fred: String(1..Max);        -- String.
   I: Integer;                  -- Size of read.
   -- Get the string.
   ReadStr(Fred, I);

   -- Echo the whole string, leading blanks trimmed.
end ReadRest2;
<<Operator Overloading In and Out Parameters.>>