如何读取文件中的某些特定列? [英] How to read some specific columns in a file?

查看:83
本文介绍了如何读取文件中的某些特定列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件 fx.txt 的前几行,内容如下:

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

我想读说第一列第三列

更新

这是我的更新代码:

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;

分开 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 。怎么了?

请注意,以上只是一段简单的代码。我可以决定稍后不在第二列中存储值。

推荐答案

过程获取 跳过任何前导空白,行终止符或页面终止符。跳过标题后,继续阅读直到 End_Of_File 为真。以三组为一组调用 Get 并丢弃中间值。

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.

附录:很多细节取决于目标。此 sscce 是在报告格式错误的数字时最小尝试获取第一列和第三列的尝试。另一种方法是将所有内容读取为 String 并使用相应版本的 Get

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;

这篇关于如何读取文件中的某些特定列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆