繁体   English   中英

阿达:从文件中读取

[英]Ada: reading from a file

我试图在Ada读取一列Long_Float值的文件,如下所示:

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;

我在编译时收到很多错误消息:

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"

文件fx.txt包含例如:

11.0
23.0
35.0
46.0

任何帮助将非常感谢。

更新的代码:

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;

但现在输出是:

1
1
.
0
2
3
.
0
3
5
.
0
4
6
.

问题是value被定义为一个character 如果我想要value Long_Float类型,以便我可以在我的程序中使用数字Long_Float 11.0, 23.0, 35.0 and 46.0 ,那么如何去做?

谢谢。

基本上,您将实例化Sequential_IO以对表示(长)浮点数的二进制值执行I / O. 这不是你文件中的内容。 您的文件包含浮点数的文本表示。

在您的示例中,去掉Sequential_IO并使用纯Text_IO.Open打开文件,使用Long_Float_Text_IO来获取Get()值。

这就是为什么你得到类型冲突错误消息,你试图在Long_Float_Text_IO File_Type变量上执行Sequential_IO操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM