我有一個文本文件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
:
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沒有被卡住。哪裏不對?
注意上面只是一段粗略的代碼。我可以決定不在後面的第二列中存儲值
@Keith Thompson我可以閱讀單列文件。我可以使用Skip_Line來忽略標題。我不知道如何使用'Set_col'過程,如果我相信這是要求轉到列號的正確方法。然後我不知道如何使用'Set_Col'並且仍然要讀取兩列。我是否應該爲每一行處理'1'列到'3'的'Set_Col'? – yCalleecharan 2012-03-26 17:33:44