简体   繁体   中英

Ada: reading from a file

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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