引发GNAT.STRING_SPLIT_INDEX_ERROR
我试着用https://marc.info/?l=gcc-补丁&m=13902261024587&w=2。然而,它怎么能读下一行呢。阿达似乎没有类似nextLine((的东西;就像在java中一样。
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.String_Split; use GNAT.String_Split;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
procedure TextFile is
File : File_Type;
Tokens : Slice_Set;
Index : Slice_Number;
type Payrate_Type is delta 0.01 range 0.00 .. 1000.00;
type Line_Info is record
Code : Unbounded_String := Null_Unbounded_String;
Department : Unbounded_String := Null_Unbounded_String;
Name : Unbounded_String := Null_Unbounded_String;
Title : Unbounded_String := Null_Unbounded_String;
ID : Natural :=0;
Payrate : Payrate_Type:=0.00;
end record;
A_Line : Line_Info;
package Payrate_IO is new Ada.Text_IO.Fixed_IO(Payrate_Type);
Last : Positive;
begin
Open (File, In_File, "Store.txt");
-- Skip the file header
Skip_Line (File);
-- Read the data
while not End_Of_File (File) loop
-- Split the line from the file on array which contains separated
-- words. Treat multiple spaces as a single separator (don't
-- create empty elements).
Create (Tokens, Get_Line (File), " ", Multiple);
-- Print each of the array's values
for I in 1 .. Slice_Count (Tokens) loop
A_Line.Code := To_Unbounded_String(Slice(Tokens, I));
A_Line.Department := To_Unbounded_String(Slice(Tokens, I+1));
A_Line.Name := To_Unbounded_String(Slice(Tokens, I+2));
A_Line.Title := To_Unbounded_String(Slice(Tokens, I+3));
A_Line.ID := Natural'Value(Slice(Tokens, I+4));
Payrate_IO.Get(Slice(Tokens,I+5),A_Line.Payrate,Last);
end loop;
end loop;
exception
when End_Error =>
if Is_Open(File) then
Close (File);
end if;
end TextFile;
Store.txt
Code Department Name/Vendor Title ID Payrate
IL Sales John Sales_person 1378 25.46
IR Crew Jesse Sales_person 1379 25.46
您的问题不在于未能阅读下一行(Get_Line (File)
做得很好(。
有问题
for I in 1 .. Slice_Count (Tokens) loop
A_Line.Code := To_Unbounded_String(Slice(Tokens, I));
A_Line.Department := To_Unbounded_String(Slice(Tokens, I+1));
A_Line.Name := To_Unbounded_String(Slice(Tokens, I+2));
A_Line.Title := To_Unbounded_String(Slice(Tokens, I+3));
A_Line.ID := Natural'Value(Slice(Tokens, I+4));
Payrate_IO.Get(Slice(Tokens,I+5),A_Line.Payrate,Last);
end loop;
你应该试着向自己解释为什么你有一个循环。
Code
在哪个切片中?PayRate
在哪个切片中?第二次循环会发生什么?