我对 Ada 非常陌生,我发现很难掌握的一件事是在 Ada 中使用文件在文件中附加一些值。对我来说,用 C 语言这样做似乎更容易。无论如何,我还没有找到好的信息,我希望有人可以在这里帮助我。
我首先声明以下内容:
PACKAGE Seq_Float_IO IS NEW Ada.Sequential_IO (Element_Type => Long_Float);
Flo_File : Seq_Long_Float_IO.File_Type;
然后创建一个文件“bvalues.dat”:
Seq_Long_Float_IO.Create(File => Flo_File, Name => "bvalues.dat");
然后编写一个名为“Largest”的变量,我使用:
Seq_Long_Float_IO.Write(File => Flo_File, Item => Largest);
我看到每次运行代码时文件“bvalues.dat”都会得到当程序运行时,它被销毁并写入新值。这对我来说没问题。我在代码中所做的就是找到某些值中的最大值并将最大元素存储在文件“bvalues.dat”中。
现在假设我必须在同一程序中使用不同的值集重复该操作(例如使用外部循环),并且我需要存储每组值的最大元素。因此,我需要能够将每个集合的每个最大值附加到文件“bvalues.dat”。如何实现这一目标?
我是否需要每次写入最大值后关闭文件“bvalues.dat”,然后再次打开它:
Seq_Long_Float_IO.Open(File => Flo_File, Mode => Append_File, Name => "bvalues.dat");
比如说在外循环中的索引递增以获取最大元素要存储的下一组值之后进行计算,然后像上面那样编写
Seq_Long_Float_IO.Write(File => Flo_File, Item => Largest); ?
新信息:
我收到错误:
40. Seq_Long_Float_IO.Open(File => Flo_File, Mode => Append_File, Name => "bvalues.dat");
|
>>> "Append_File" is not visible
>>> non-visible declaration at a-sequio.ads:58, instance at line 8
>>> non-visible declaration at a-textio.ads:56
非常感谢...
测试文件:
WITH Ada.Text_IO;
WITH Ada.Sequential_IO;
PROCEDURE TestWrite5 IS
PACKAGE Seq_Float_IO IS NEW Ada.Sequential_IO (Element_Type => Float);
Flo_File : Seq_Float_IO.File_Type;
BEGIN
Seq_Float_IO.Open (File => Flo_File, Mode => Seq_Float_IO.Append_File,
Name =>"bvalues.dat");
exception
when Name_Error =>
Create (File => Flo_File, Mode => Out_File, Name => "bvalues.dat");
END TestWrite5;
编译时我得到:
-
异常
当Name_Error =>时
|
<块引用>
<块引用>
<块引用>
“Name_Error”不可见
a-sequio.ads:111 处的不可见声明,第 5 行的实例
a-textio.ads 上的不可见声明:298
a-ioexce.ads 上的不可见声明:23
创建(文件=> Flo_File,模式=> Out_File,名称=>“bvalues.dat”);
|
<块引用>
<块引用>
<块引用>
“创建”不可见
a-sequio.ads:73 处的不可见声明,第 5 行的实例
a-textio.ads 上的不可见声明:90
15.
如果我还放置 Seq_Float_IO.Out_File 而不仅仅是 Out_File,则不会改变。
I'm very new to Ada and one thing that I find hard to grasp is working with Files in Ada when it comes to append some values in a file. It seems easier for me to do so in C. Anyway, I haven't found good information and I hope someone could help me here.
I declare the following first:
PACKAGE Seq_Float_IO IS NEW Ada.Sequential_IO (Element_Type => Long_Float);
Flo_File : Seq_Long_Float_IO.File_Type;
and then I create a file "bvalues.dat":
Seq_Long_Float_IO.Create(File => Flo_File, Name => "bvalues.dat");
and then to write say a variable named "Largest", I use:
Seq_Long_Float_IO.Write(File => Flo_File, Item => Largest);
I see that every time I run the code the file "bvalues.dat" gets destroyed and new values are written to it as the program runs. This is ok for me. What I'm doing in my code is to find the largest value of some values and store the largest element in the file "bvalues.dat".
Now say I have to repeat the operation with different sets of values IN THE SAME PROGRAM (say with an outer LOOP) and I need to store the largest element of each set of values. Thus I need to be able to append each largest value of every set to the file "bvalues.dat". How to achieve this?
Do I need to close the file "bvalues.dat" each time after writing a largest value and then open it again:
Seq_Long_Float_IO.Open(File => Flo_File, Mode => Append_File, Name => "bvalues.dat");
say after an index in an outer loop gets incremented to take in the next set of values for which the largest element is to be computed and then write as I did above
Seq_Long_Float_IO.Write(File => Flo_File, Item => Largest); ?
NEW INFO:
I get the error:
40. Seq_Long_Float_IO.Open(File => Flo_File, Mode => Append_File, Name => "bvalues.dat");
|
>>> "Append_File" is not visible
>>> non-visible declaration at a-sequio.ads:58, instance at line 8
>>> non-visible declaration at a-textio.ads:56
Thanks a lot...
Test file:
WITH Ada.Text_IO;
WITH Ada.Sequential_IO;
PROCEDURE TestWrite5 IS
PACKAGE Seq_Float_IO IS NEW Ada.Sequential_IO (Element_Type => Float);
Flo_File : Seq_Float_IO.File_Type;
BEGIN
Seq_Float_IO.Open (File => Flo_File, Mode => Seq_Float_IO.Append_File,
Name =>"bvalues.dat");
exception
when Name_Error =>
Create (File => Flo_File, Mode => Out_File, Name => "bvalues.dat");
END TestWrite5;
On compiling I get:
-
exception
-
when Name_Error =>
|
"Name_Error" is not visible
non-visible declaration at a-sequio.ads:111, instance at line 5
non-visible declaration at a-textio.ads:298
non-visible declaration at a-ioexce.ads:23
-
Create (File => Flo_File, Mode => Out_File, Name => "bvalues.dat");
|
"Create" is not visible
non-visible declaration at a-sequio.ads:73, instance at line 5
non-visible declaration at a-textio.ads:90
15.
It doesn't change if I also put Seq_Float_IO.Out_File instead of just Out_File.
发布评论
评论(1)
创建,顾名思义,将创建一个全新的文件,即使该文件已经存在。
如果文件已存在并且您想要追加到该文件,则可以使用
Open
。如果您想打开它进行追加,但如果它不存在,则创建它,通常的习惯用法是将
Create
调用放在Open
周围的异常处理程序中,例如所以:从文本的其余部分来看,您似乎正在考虑将临时值存储在文件中。除非您出于某种原因需要持久性(从崩溃中恢复等),否则我不会这样做。磁盘 IO 非常非常 非常 慢。只需将临时值保存在变量中并在获得结果时保存结果即可。
Create, like the name implies, will create a brand new file, even if one already exists.
If the file already exists and you want to append to it, you would use
Open
.If you want to open it for appending, but if it doesn't exist create it, the normal idiom is to put the
Create
call in an exception handler aroundOpen
, like so:From the rest of your text, it looks like you are thinking about storing temp values in a file. I wouldn't do that unless you need persistence for some reason (recovering from crashes, etc). Disk IO is way way way slow. Just keep your temp values in a variable and save the result when you have it.