How to read some specific columns in a file?

2019-08-05 07:40发布

问题:

I have the first few lines of a text file fx.txt with the following contents:

t(ms) ForceX(N)  ForceY(N)
 0.0     10.0      20.0
 1.0     15.0      10.9
 2.0     12.0      30.0

I would like to read say the contents of the first column and of the third column. How to go about in Ada?

Update

Here's my updated code:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Long_Float_Text_IO;
with Ada.IO_Exceptions;


procedure Get_Projections is

   Input_File                : File_Type;
   Value                     : Long_Float;


   procedure Open_Data_Read (File : in out  Ada.Text_IO.File_Type;
                             Name    : in String;
                             Success : out Boolean) is separate;
   Success                   : Boolean;


begin

   Open_Data_Read (File => Input_File, Name => "fx.txt", Success => Success);
       if not Success then
          return;
       end if;

   Ada.Text_IO.Skip_Line(File => Input_File, Spacing => 1);

   while not End_Of_File (Input_File) loop

     begin
       Ada.Long_Float_Text_IO.Get (File => Input_File, Item => Value);
       exception
          when ADA.IO_EXCEPTIONS.DATA_ERROR =>
       Ada.Text_IO.Put_Line (" Data error");
     end;
     Ada.Long_Float_Text_IO.Put (Item => Value, Fore => 3, Aft  => 5, Exp  => 0);

     begin
       Ada.Long_Float_Text_IO.Get (File => Input_File, Item => Value);
       exception
          when ADA.IO_EXCEPTIONS.DATA_ERROR =>
       Ada.Text_IO.Put_Line (" Data error");
     end;
     Ada.Long_Float_Text_IO.Put (Item => Value, Fore => 3, Aft  => 5, Exp  => 0);

     begin
       Ada.Long_Float_Text_IO.Get (File => Input_File, Item => Value);
       exception
          when ADA.IO_EXCEPTIONS.DATA_ERROR =>
       Ada.Text_IO.Put_Line (" Data error");
     end;
     Ada.Long_Float_Text_IO.Put (Item => Value, Fore => 3, Aft  => 5, Exp  => 0);


   end loop;
   Ada.Text_IO.Close (File => Input_File);
   Ada.Text_IO.Put_Line (Item => "Reading file success: " & Boolean'Image (Success));


end Get_Projections;

and the separate Open_Data_Read.adb:

separate (get_projections)

procedure Open_Data_Read (File : in out  Ada.Text_IO.File_Type;
                 Name : in String; Success : out Boolean) is

   --this procedure prepares a file for reading
     begin
      Success := True;
      begin
         Ada.Text_IO.Open
           (File => File,
            Mode => Ada.Text_IO.In_File,
            Name => Name);
      exception
         when Ada.Text_IO.Name_Error  =>
         Success := False;
         Ada.Text_IO.Put (File => Standard_Error, Item => "****File not found....****");
         Ada.Text_IO.Put_Line (Item => "Reading file success: " & Boolean'Image (Success));
  end;
end Open_Data_Read;

Exceptions data_error are not caught. What is wrong?

Note the above is just a crude piece of code. I can decide not to store the values in the second column later on

回答1:

procedure Get "skips any leading blanks, line terminators, or page terminators." Once you skip the header, just keep reading until End_Of_File is true. Call Get in groups of three and discard the middle value.

Addendum: Much of the detail depends on the goal. This sscce is a minimal attempt to acquire columns one and three, while reporting malformed numbers. The alternative is to read everything as a String and use the corresponding version of Get.

with Ada.Text_IO;
with Ada.Float_Text_IO;

procedure Get_Data is

   package TIO renames Ada.Text_IO;
   package FIO renames Ada.Float_Text_IO;

   Data  : TIO.File_Type;
   Value : Float;

begin
   TIO.Open (Data, TIO.In_File, "data.txt");
   TIO.Skip_Line (Data);
   while not TIO.End_Of_File (Data) loop
        FIO.Get (Data, Value);
        FIO.Put (Value, 3, 5, 0); TIO.Put (" ");
        FIO.Get (Data, Value); -- Discard
        FIO.Get (Data, Value);
        FIO.Put (Value, 3, 5, 0); TIO.New_Line;
   end loop;
   exception
      when TIO.End_Error => null;
      when TIO.Data_Error =>
         TIO.Put_Line ("Data error: " & TIO.Name (Data));
end Get_Data;


标签: ada