โŒ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayAda Forum - Latest topics

Regarding END_ERROR when using Ada.Text_IO.Set_Line on IN_FILE

By: Reed
21 November 2023 at 11:17

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

Read full topic

[Beginner] A question regarding reading from keyboard

By: Reed
6 September 2023 at 05:44

Hello, I am a beginner at Ada and programming in general. Currently,
I am practising by writing programs that perform operations on text I input via
the keyboard.
I have several questions about the best practices for doing so.

The code below is my learning attempt for different methods for reading:

with Ada.Text_IO; use Ada.Text_IO;
--This program will echo what the user types in.
procedure Main is
   input : Character;
   line_end : Boolean := False;
begin

   Put_Line("Enter some characters.");
   Put_Line("Output is character by character.");
   loop
      Get(input);
      Put(input);
      exit when End_Of_Line;
   end loop;
   New_Line;

   Put_Line("This uses strings.");
   Put_Line("Enter a sentence: ");
   
   ------------------
   Look_Ahead (Item        => input,
               End_Of_Line => line_end);
   
   if line_end then
      Put_Line ("Arr, end of the line matey");
   else
      Put (input);
   end if;
   ------------------
   declare
      s : String := Get_Line;
   begin
      
      Put_Line(s);
   end;
end Main;

I noticed that the declaration for the String โ€˜sโ€™ would read a new line and end the program without myself being able to type any characters.

Question 1: Which part of the code is resulting in a new line being left to read for the Get_Line function? If I add the Skip_Line procedure just before the new line check, I dont have any issues.

Question 2: What is the best practice for reading in things from the keyboard?

I appreciate any hints. Thank you.

3 posts - 3 participants

Read full topic

โŒ
โŒ