Ada: reading from a file

荒凉一梦 提交于 2019-12-07 17:00:58

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/9792230/ada-reading-from-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!