Regarding END_ERROR when using Ada.Text_IO.Set_Line on IN_FILE
When using the procedure Set_Line (File, X) after using the procedure Skip_Line(File, Y) the error, END.ERROR is raised when X <= Y
(except when X = Y = 1).
When the procedure Set_Line (File, X) is called on a File of mode IN_FILE, it internally calls Skip_Line until the file line number reaches X.
If X is not equal than the current line in the file, the procedure will continuously skip lines until it reaches another page with a matching line. (RM 2022 p.g.523)
I am curious about the reasoning for this functionality as it has caused an amusing problem in the trivial program I am writing.
The program will get the number of lines from a .txt file and select a random line to print to the console. A word of the day program.
Minimal Example of Issue:
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
File_Name : constant String := "TEST.txt";
Test_File : File_Type;
begin
Create (Test_File, Out_File, File_Name);
for I in 1 .. 10 loop
Put_Line (Test_File, "Line :" & Integer'Image (I));
end loop;
Close (Test_File);
Open (Test_File, In_File, "Test.txt");
Skip_Line (Test_File, 5);
Set_Line (Test_File, 4);
end Main;
--Will raise End_Error
I would think that the procedure would check if X < File.Line then perform further actions. While am able to work around this, I am interested to understand the reasoning.
Thanks.
9 posts - 5 participants