From: "J-P. Rosen" <rosen@adalog.fr>
Subject: Re: Creating an empty file with Ada.Text_IO
Date: Tue, 25 Aug 2015 18:18:54 +0200
Date: 2015-08-25T18:18:54+02:00 [thread overview]
Message-ID: <mri4e7$a7k$1@dont-email.me> (raw)
In-Reply-To: <1c1a3b38-4810-4002-b4f5-ec09b5f97041@googlegroups.com>
Le 25/08/2015 17:26, Maciej Sobczak a écrit :
>> 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;
Or simply:
A : Ada.Streams.Stream_Element_Array
(Stream_Element_Offset(S'First) ..
Stream_Element_Offset(S'Last));
(saves some computation in the loop)
--
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr
next prev parent reply other threads:[~2015-08-25 16:18 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
2015-08-25 16:18 ` J-P. Rosen [this message]
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