comp.lang.ada
 help / color / mirror / Atom feed
From: Maciej Sobczak <see.my.homepage@gmail.com>
Subject: Re: Creating an empty file with Ada.Text_IO
Date: Tue, 25 Aug 2015 08:26:24 -0700 (PDT)
Date: 2015-08-25T08:26:24-07:00	[thread overview]
Message-ID: <1c1a3b38-4810-4002-b4f5-ec09b5f97041@googlegroups.com> (raw)
In-Reply-To: <fa3d6ff4-d111-4564-a55f-33b2c0b076fa@googlegroups.com>


>    procedure Write_String_To_File (File_Name : in String; S : in String) is
>       A : Ada.Streams.Stream_Element_Array (1 .. S'Length);
>       F : Ada.Streams.Stream_IO.File_Type;
>    begin
>       -- convert S to stream elements
>       for I in S'Range loop
>          A (Ada.Streams.Stream_Element_Offset (I)) :=
>             Ada.Streams.Stream_Element (Character'Pos (S (I)));
>       end loop;

Before anybody uses it in a rocket - there is no provision above that S'First will be 1 and the indices might not be compatible in the above assignment.

Correct version is:

      for I in S'Range loop
         A (Ada.Streams.Stream_Element_Offset (I - S'First + 1)) :=
            Ada.Streams.Stream_Element (Character'Pos (S (I)));
      end loop;

And there are actually several ways to write it.
Now this procedure can write String slices that do not begin at index 1.

(Frankly, this part of Ada is inherently error-prone. I stumble on it repeatedly.)

-- 
Maciej Sobczak * http://www.inspirel.com

  reply	other threads:[~2015-08-25 15:26 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-24 14:16 Creating an empty file with Ada.Text_IO Maciej Sobczak
2015-08-24 16:15 ` Dmitry A. Kazakov
2015-08-25  8:20   ` Maciej Sobczak
2015-08-25 15:26     ` Maciej Sobczak [this message]
2015-08-25 16:18       ` J-P. Rosen
2015-08-25 16:45       ` G.B.
2015-08-24 18:51 ` AdaMagica
2015-08-24 22:22   ` Randy Brukardt
2015-08-25  0:06 ` Dennis Lee Bieber
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox