如何读取文件中的某些特定列?

时间:2012-03-26 17:11:34

标签: ada

我有文本文件fx.txt的前几行,其中包含以下内容:

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

我想读一下first columnthird column的内容。怎么去阿达?

更新

这是我更新的代码:

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;

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;

未捕获异常data_error 。有什么问题?

请注意,上面只是一段粗略的代码。我可以决定稍后在

中将值存储在第二列中

1 个答案:

答案 0 :(得分:2)

procedure Get“跳过任何前导空格,行终止符或页面终结符。”跳过标题后,请继续阅读,直到End_Of_File为真。以三个为一组调用Get并丢弃中间值。

附录:许多细节取决于目标。此sscce是获取第一列和第三列的最小尝试,同时报告格式错误的数字。另一种方法是将所有内容都读作String并使用相应版本的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;