我有一个文本文件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