Re: Lenght check failed
[ Nouvelle discussion
| Répondre au groupe
|
fr.comp.lang.ada ]
On 9 fév, 15:09, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote:
> Tu peux déclarer une String sans spécifier sa taille à condition de
> l'initialiser immédiatement avec une valeur... par exemple, le
> résultat d'Ada.Strings.Unbounded.To_String.
>
> with Ada.Strings.Unbounded;
> procedure P is
> Keyboard_Input : constant Ada.Strings.Unbounded.Unbounded_String :>
Ada.Strings.Unbounded.Get_Line; -- longueur inconnue
> S : constant String := Ada.Strings.Unbounded.To_String (Keyboard_Input);
> begin
> ...
> end P;
>
> Maintenant si tu veux un tableau de chaînes de longueurs variables
> ("ragged array") il te faut un tableau de valeurs accès vers des
> String. Par exemple:
>
> with Ada.Strings.Unbounded;
> with Ada.Unchecked_Deallocation;
> procedure Q is
>
> type String_Access is access String;
> type String_Array is array (Positive range <>) of String_Access;
>
> procedure Free is
> new Ada.Unchecked_Deallocation (Object => String, Name => String_Access);
>
> Keyboard_Input : Ada.Strings.Unbounded.Unbounded_String;
> Inputs : String_Array (1 .. 10);
> begin
> Read_From_Keyboard: for J in Inputs'Range loop
> Keyboard_Input := Ada.Strings.Unbounded.Get_Line; -- unknown length
> Input (J) :> new String'(Ada.Strings.Unbounded.To_String
(Keyboard_Input));
> -- Input (J) is an access to a new string allocated on the heap with the
> -- proper size
> end loop Read_From_Keyboard;
>
> -- process Inputs here
>
> Deallocate_Each_String: for J in Inputs'Range loop
> Free (Inputs (J));
> end loop Deallocate_Each_String;
> end Q;
>
> Voir aussi:
>
>
http://en.wikibooks.org/wiki/Ada_Programming/Stringshttp://fr.wikibooks.org/wiki/Programmation_Ada/FAQ/(questions
2.17 et 2.6.)
>
> --
> Ludovic Brenta.
Merci pour les liens, je vais regarder ca.
Pour s, j'ai fait un s : string :Ada.Strings.Unbounded.To_String(Str);
Merci bcp pour l'aide :)

|
 cette fonctionnalité est reservée aux membres ayant une session active !
|