Ada:从文件中读取 [英] Ada: reading from a file
问题描述
我试图读取 Ada
中的 Long_Float
值的单个文件,如下所示: p>
与Ada.Text_IO;使用Ada.Text_IO;
与Ada.Long_Float_Text_IO;
与Ada.Sequential_IO;
过程Test_Read是
包Seq_Float_IO是新的Ada.Sequential_IO(Element_Type => Long_Float);
Input_File:File_Type;
值:Long_Float;
begin
Seq_Float_IO.Open(File => Input_File,Mode => Seq_Float_IO.In_File,Name =>fx.txt);
而不是End_OF_File(Input_File)循环
Seq_Float_IO.Read(Input_File,value);
Ada.Long_Float_Text_IO.Put(Item => value,Fore => 3,Aft => 5,Exp => 0);
结束循环;
Seq_Float_IO.close(File => Input_File);
end Test_Read;
我在编译时收到很多错误消息:
17。 Seq_Float_IO.Open(File => Input_File,Mode => Seq_Float_IO.In_File,Name =>fx.txt);
|
>>>期望在第10行的实例的私人类型Ada.Sequential_Io.File_Type
>>>找到私人类型Ada.Text_Io.File_Type
18.而不是End_OF_File(Input_File)循环
19. Seq_Float_IO.Read(Input_File,value);
|
>>>期望在第10行的实例的私人类型Ada.Sequential_Io.File_Type
>>>找到私人类型Ada.Text_Io.File_Type
文件 fx.txt 包含例如:
11.0
23.0
35.0
46.0
任何帮助都将得到最多赞赏。
<使用Ada.Text_IO更新代码:
使用Ada.Text_IO;
过程Test_Read是
Input_File:File_Type;
值:字符;
begin
Ada.Text_IO.Open(File => Input_File,Mode => Ada.Text_IO.In_File,Name =>fx.txt);
而不是End_OF_File(Input_File)循环
Ada.Text_IO.Get(File => Input_File,Item => value);
Ada.Text_IO.Put(Item => value);
Ada.Text_IO.New_Line;
结束循环;
Ada.Text_IO.Close(File => Input_File);
end Test_Read;
但现在输出结果是:
1
1
。
0
2
3
。
0
3
5
。
0
4
6
。
问题在于 value
被定义为一个字符
。如果我希望 value
的类型为 Long_Float
,那么我可以使用数字 11.0 ,23.0,35.0和46.0
后来在我的程序中,那么怎么去?
谢谢。
基本上,你正在实例化Sequential_IO来对表示(长)浮点数的二进制值做I / O操作。这不是你的文件中的内容。你的文件包含浮点数字的文本表示。在你的例子中,摆脱Sequential_IO并使用普通的Text_IO.Open打开文件,Long_Float_Text_IO到Get()值
$ b
这就是为什么你会得到类型冲突错误消息,你试图对Long_Float_Text_IO File_Type变量执行Sequential_IO操作。
I am trying to read a file with a single column of Long_Float
values in Ada
as follows:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Long_Float_Text_IO;
with Ada.Sequential_IO;
procedure Test_Read is
package Seq_Float_IO is new Ada.Sequential_IO (Element_Type => Long_Float);
Input_File : File_Type;
value : Long_Float;
begin
Seq_Float_IO.Open (File => Input_File, Mode => Seq_Float_IO.In_File, Name => "fx.txt");
while not End_OF_File (Input_File) loop
Seq_Float_IO.Read (Input_File, value);
Ada.Long_Float_Text_IO.Put (Item => value, Fore => 3, Aft => 5, Exp => 0);
end loop;
Seq_Float_IO.close (File => Input_File);
end Test_Read;
I do get lots of error messages on compilation:
17. Seq_Float_IO.Open (File => Input_File, Mode => Seq_Float_IO.In_File, Name => "fx.txt");
|
>>> expected private type "Ada.Sequential_Io.File_Type" from instance at line 10
>>> found private type "Ada.Text_Io.File_Type"
18. while not End_OF_File (Input_File) loop
19. Seq_Float_IO.Read (Input_File, value);
|
>>> expected private type "Ada.Sequential_Io.File_Type" from instance at line 10
>>> found private type "Ada.Text_Io.File_Type"
The file fx.txt
contains for example:
11.0
23.0
35.0
46.0
Any help will be most appreciated.
Updated Code:
with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Read is
Input_File : File_Type;
value : Character;
begin
Ada.Text_IO.Open (File => Input_File, Mode => Ada.Text_IO.In_File, Name => "fx.txt");
while not End_OF_File (Input_File) loop
Ada.Text_IO.Get (File => Input_File, Item => value);
Ada.Text_IO.Put (Item => value);
Ada.Text_IO.New_Line;
end loop;
Ada.Text_IO.Close (File => Input_File);
end Test_Read;
But now the output is:
1
1
.
0
2
3
.
0
3
5
.
0
4
6
.
The problem is that value
is defined as a character
. If I want value
to be of the type Long_Float
so that I can use the numbers 11.0, 23.0, 35.0 and 46.0
later on in my program, then how to go about?
Thanks.
Basically, you're instantiating Sequential_IO to do I/O on binary values representing (long) floating point numbers. That's not what's in your file. Your file contains textual representations of floating point numbers.
In your example, get rid of Sequential_IO and use the plain Text_IO.Open to open the file and Long_Float_Text_IO to Get() values.
This is why you're getting the type conflict error messages, you're attempting to execute Sequential_IO operations on a Long_Float_Text_IO File_Type variable.
这篇关于Ada:从文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!