나는 다음 내용을 텍스트 파일 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
의 내용을 말하고 읽고 싶습니다. 어떻게 에이다에 갈거야?
업데이트
여기 내 업데이트 된 코드는 다음과 같습니다
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;
예외. 뭐가 잘못 되었 니?
위의 코드는 단순한 코드 일뿐입니다. 나중에 두 번째 열에 값을 저장하지 않기로 결정할 수 있습니다.
@Keith Thompson 단일 열로 파일을 읽을 수 있습니다. 'Skip_Line'을 사용하여 헤더를 무시할 수 있습니다. 나는 이것이 열 번호로 가도록 요청하는 올바른 방법이라고 생각한다면'Set_col' 프로 시저를 어떻게 사용할 지 모른다. 그럼'Set_Col'을 어떻게 사용하는지 모르겠다. 각 행에 대해 'Set_Col' 열을'1' 열에서 '3' 열까지 바꿔야하나요? – yCalleecharan