comp.lang.ada
 help / color / mirror / Atom feed
From: Ian Clifton <ian@melvin.dp.ox.ac.uk>
Subject: Re: CONTRAINT ERROR (was Re: parsing a string)
Date: 2000/01/26
Date: 2000-01-26T13:39:05+00:00	[thread overview]
Message-ID: <4qaeltvtop.fsf@melvin.dp.ox.ac.uk> (raw)
In-Reply-To: 86mm2o$3cd$1@nnrp1.deja.com

pumilia@est.it writes:

>  output_file_string : unbounded_string ;
>  output_file : string (1..25) ;
>  pos1 : natural;
> 
> Begin
> 
>     get_line(output_file_string);
> 
>    put(" output:  "); put(output_file_string); put("<--"); new_line;
>    output_file :=  to_string(output_file_string);
>    put(" Output:  "); put(output_file); put("<--"); new_line;
>    put(" index:  "); pos1 := Index(output_file,"xxx");
>    put(pos1);
>    new_line;
> 
> end str2_test;
> -- str2_test --------------------------------------
> 
> 
> While executing i get the error:
> 
> >   raised constraint_error : str2_test.adb:24
> 
> This is line 24:
> 
>   output_file :=  to_string(output_file_string);
> 
> 
> Any help wil be appreciiated
> thank you
> Pol

I think the problem is that output_file is a fixed-length(=25) string,
while Ada.Strings.Unbounded.To_String returns a fixed string of
whatever length output_file_string happens to be. This will fail
unless that length happens to be 25. In places where you'd like to
assign to a fixed-length string, it almost always makes more sense to
use Ada.Strings.Fixed.Move instead. Ada.Strings.Fixed.Move pads or
truncates appropriately to match the length of the target string.

-- 
Ian Clifton                   Phone: +44 1865 275631
Dyson Perrins Laboratory      Fax:   +44 1865 275674
Oxford University  OX1 3QY UK ian.clifton@linacre.ox.ac.uk




  parent reply	other threads:[~2000-01-26  0:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-01-15  0:00 parsing a string Paolo M. Pumilia
2000-01-15  0:00 ` James S. Rogers
2000-01-24  0:00   ` pumilia
2000-01-24  0:00   ` pumilia
2000-01-24  0:00     ` Stephen Leake
2000-01-26  0:00       ` CONTRAINT ERROR (was Re: parsing a string) pumilia
2000-01-26  0:00         ` John English
2000-01-26  0:00         ` Roger Racine
2000-01-26  0:00           ` Fraser
2000-01-26  0:00             ` Simon Wright
2000-01-26  0:00         ` Ian Clifton [this message]
2000-01-24  0:00     ` parsing a string Ehud Lamm
replies disabled

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