This question is a follow-up of the post at
Ada file operation: instantiation and exception
about writing to files in Ada.
I chose to place this question in a separate post so that it'll become visible to more people as I already accepted an answer on a slightly different issue (which was on exceptions in file handling) in that aforementioned post.
WITH Ada.Sequential_IO;
WITH Ada.Float_Text_IO;
PROCEDURE TEST is
package Seq_Float_IO is new Ada.Sequential_IO (Element_Type => Float);
X_File : Seq_Float_IO.File_Type;
File_Name : String;
procedure Open_Data(File : in out Seq_Float_IO.File_Type;
Name : in String) is
BEGIN
begin
Seq_Float_IO.Open (
File => File,
Mode => Seq_Float_IO.Append_File,
Name => File_Name );
exception
when Seq_Float_IO.Name_Error =>
Seq_Float_IO.Create (
File => File,
Mode => Seq_Float_IO.Out_File,
Name => File_Name);
end;
END Open_Data;
x : CONSTANT Float := 2.0;
BEGIN --main program
Open_Data(X_File, "xvalues.dat");
Seq_Float_IO.Write(File => X_File,Item => x);
Seq_Float_IO.Close(File => X_File);
END TEST;
On compiling the above I get an error as follows:
- X_File : Seq_Float_IO.File_Type;
- File_Name : String;
|
unconstrained subtype not allowed (need initialization) provide initial value or explicit array bounds
- File_Name : String;
|
I don't know 2 things:
- I have File_Name : String; as I want to be able to write to different files. So I want a general string and not something like:
File_Name : CONSTANT String := "one_File_Only.dat"
- Would it be better to save the procedure Open_Data in separate ads and adb (for the body) files?
Thanks a lot...
NEW...
I've modified the code as follows:
WITH Ada.Sequential_IO;
PROCEDURE TEST1 is
package Seq_Float_IO is new Ada.Sequential_IO (Element_Type => Float);
X_File, Y_File : Seq_Float_IO.File_Type;
Name_X : CONSTANT String := "domainvalues.dat";
Name_Y : CONSTANT String := "ordinatevalues.dat";
procedure Open_Data(File : in out Seq_Float_IO.File_Type; Name : in String) is
BEGIN
begin
Seq_Float_IO.Open (
File => File,
Mode => Seq_Float_IO.Append_File,
Name => Name_X );
exception
when Seq_Float_IO.Name_Error =>
Seq_Float_IO.Create (
File => File,
Mode => Seq_Float_IO.Out_File,
Name => Name_X);
end;
END Open_Data;
x : CONSTANT Float := 2.0;
BEGIN --main program
Open_Data(File => X_File, Name => Name_X);
Seq_Float_IO.Write(File => X_File, Item => x);
Seq_Float_IO.Close(File => X_File);
Open_Data(File => Y_File, Name => Name_Y);
Seq_Float_IO.Write(File => Y_File, Item => x);
Seq_Float_IO.Close(File => Y_File);
END TEST1;
As you see I have
Seq_Float_IO.Open (
File => File,
Mode => Seq_Float_IO.Append_File,
Name => Name_X );
I have put Name_X as the parameter that Name is taking but this is not right as I should be able to pass in a general name which can be either Name_X or Name_Y. Sorry guys, I can't figure out what to put here.
I would much appreciate your help. Thanks