は、私は次の内容のテキストファイル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 column
のとthird column
の内容を言って読みたいです。 Adaでどうやって行くの?
更新
ここに私の更新されたコードです:
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
:をキャッチされていませんdata_error
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;
例外。なにが問題ですか?
上記のコードは単なるコードです。私は後で2列目の値を格納しないことにして、
@Keith Thompson私は1つの列でファイルを読むことができます。 'Skip_Line'を使ってヘッダを無視することができます。私は 'Set_col'プロシージャをどのように使うべきかわかりませんが、これはカラム番号に行くのが正しい方法だと思っています。次に、 'Set_Col'の使い方を知らず、2つのカラムを読み込ませるようにします。私は、 'Set_Col'を列' 1'から '3'までそれぞれの行について扱うべきでしょうか? – yCalleecharan