views:

117

answers:

2
+2  A: 

The thing about plain String in Ada is that a particular string, like your File_Name, has to be fixed-length; but different Strings can be of different length.

You can write

S1 : String := "1234";
S2 : String := "12345";

in which case S1 is of length 4, and assignments to it have to be of length 4. You can write

S1 := "abcd";

but if you try to write

S1 := "pqrst";

or

S1 := S2;

you will get a Constraint_Error.

In the case of String parameters to subprograms, like your Open_Data, the String parameter Name takes on the length -- and of course the value! of the actual parameter in the call. So you can say

Open_Data (X_File, "x.dat");
Open_Data (Y_File, "a_very_long_name.dat");

You were having problems earlier with

procedure Open_Data(File : in out Seq_Float_IO.File_Type; 
                    Name : in String) is
begin
   Seq_Float_IO.Open (File => File,
                      Mode => Seq_Float_IO.Append_File,
                      Name => ????);

I'm reluctant to just tell you the answer, so -- consider the File => File part. The first File is the name of the formal parameter of Seq_Float_IO.Open and the second File is what is to be passed, in this case Open_Data's File parameter.

It might help if I point out that I could have written the calls above as

Open_Data (File => X_File, Name => "x.dat");
Open_Data (File => Y_File, Name => "a_very_long_name.dat");
Simon Wright
+1 for the Socratic approach; I am too impatient! :-)
trashgod
Thanks. Please see my updated post. I still have problems.
yCalleecharan
Thanks for the string explanation. I've seen in a book an example containing: File_Name : CONSTANT String := "one_File_Only.dat" as I mentioned in my post...but of course it would be helpful to extend this to any string. Thanks again.
yCalleecharan
+1  A: 

@Simon Wright's answer is correct, and you may find it helpful to compare his answer to the second one I wrote earlier. Note that if you had

Name_X : constant String := "domainvalues.dat";
Name_Y : constant String := "ordinatevalues.dat";

Either string, Name_X or Name_Y, could be used as the actual Name parameter to Open_Data. The formal parameter, Name, is of type String. String is unconstrained, and it may be any (implementation-defined) maximum length. In contrast, Name_X and Name_Y each have a fixed length determined by their initial assignment.

Addendum: You wrote a subprogram with a formal parameter (Name) of type String, having this signature

procedure Open_Data(
    File : in out Seq_Float_IO.File_Type;
    Name : in String) is ...

In the implementation, you want to forward to Open the String you received as the actual parameter (Name), not the name of a global constant (Name_X).

Seq_Float_IO.Open (
    File => File,
    Mode => Seq_Float_IO.Append_File,
    Name => Name );
trashgod
Thanks. Please see my updated post. I still have problems.
yCalleecharan
Thanks very much for your patience with me. Both Simon Wright and you have given me the right answer and a lot of help. As I already accepted an answer from you for my earlier post, I'm accepting Simon Wright's answer this time. I hope it's ok with you. He also have a much less reputation score than you do. Thanks again.
yCalleecharan
Yes I know that putting Name_X defeats the point of creating a procedure for the open and write process. I just didn't know what to put there. File IO in Ada seems hard and I need to study this part more carefully. Examples in books are not comprehensive...they tend to stick to straighforward file operations.
yCalleecharan
I gave you a vote up
yCalleecharan
@yCalleecharan: I agree completely with your accepting @Simon Wright's answer. He is a respected contributor in several fora, and I urge you to also up-vote his answer, as I did. I'm glad you are making progress in Ada!
trashgod
Done for the up-vote. I'm starting to really like Ada. I was using C mainly and Matlab. Ada helps to avoid mistakes that can creep in easily in other languages in scientific computing. There are however few textbooks out there which show applications of Ada in numerical computing. Ada 2005 brings in new facilities for matrix operations such as calculations of eigenvalues and this is on my to-do exploration list. Thanks again.
yCalleecharan
I especially like Ada verbose style. Coming from C, Ada feels wordy but this helps a lot in program maintainability. Now I'm taking the good habit of writing long and meaningful variable names also, a thing which I was not doing when writing in C.
yCalleecharan
@yCalleecharan: Excellent! You may also enjoy these Ada examples: http://sites.google.com/site/drjohnbmatthews/
trashgod
Thanks for the link.
yCalleecharan