file - Ada:从文件中读取

标签 file ada

我正在尝试读取 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 的类型为 Long_Float,以便稍后在程序中使用数字 11.0、23.0、35.0 和 46.0那怎么办呢?

谢谢。

最佳答案

基本上,您正在实例化 Sequential_IO 以对表示(长) float 的二进制值执行 I/O。这不是你的文件中的内容。您的文件包含 float 的文本表示形式。

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

这就是您收到类型冲突错误消息的原因,您正在尝试对 Long_Float_Text_IO File_Type 变量执行 Sequential_IO 操作。

关于file - Ada:从文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9792230/

相关文章:

android - 无法使用 OkHttp3 在文件管理器中选择文件

c - 将数据文件作为链表中的节点读取的函数

ada - 打破测试功能的 Ada 隐私

ada - 即使在程序结束时断言并为相同的条件,也不能证明程序的后置条件

c++ - 读取二进制文件的惯用 C++17 标准方法是什么?

java - 将文档写入文件 jsoup

python - re.search 无法在文件中找到正则表达式模式

ada - Ada 中的范围检查和长度检查(SPARK 模式)

linux - 在 linux 终端运行 Ada 程序

c++ - 使用 C 或 C++(或 Ada 95)进行 Windows LDAP 组用户检查