In this section, we will explore how to handle input and output (I/O) in Ada. I/O operations are fundamental for interacting with users, reading from files, and writing data to various outputs. We will cover the following topics:

  1. Basic I/O Operations
  2. Formatted I/O
  3. File I/O

  1. Basic I/O Operations

Reading Input

To read input from the user, Ada provides the Get procedure. Here is a simple example:

with Ada.Text_IO; use Ada.Text_IO;

procedure Read_Input is
   Name : String (1 .. 50);
   Length : Natural;
begin
   Put("Enter your name: ");
   Get_Line(Name, Length);
   Put_Line("Hello, " & Name(1 .. Length) & "!");
end Read_Input;

Explanation:

  • Ada.Text_IO is the package that provides basic I/O functionalities.
  • Get_Line reads a line of text from the standard input.
  • Put and Put_Line are used to display text on the standard output.

Writing Output

To write output to the console, you can use the Put and Put_Line procedures:

with Ada.Text_IO; use Ada.Text_IO;

procedure Write_Output is
begin
   Put("This is a simple output.");
   Put_Line("This is another line of output.");
end Write_Output;

Explanation:

  • Put writes text without a newline at the end.
  • Put_Line writes text followed by a newline.

  1. Formatted I/O

Ada allows for formatted I/O operations, which can be useful for displaying data in a structured manner.

Example: Formatted Output

with Ada.Text_IO; use Ada.Text_IO;

procedure Formatted_Output is
   Number : Integer := 12345;
   Pi     : Float := 3.14159;
begin
   Put_Line("Formatted Output Example:");
   Put("Integer: ");
   Put(Integer'Image(Number));
   New_Line;
   Put("Float: ");
   Put(Float'Image(Pi));
   New_Line;
end Formatted_Output;

Explanation:

  • Integer'Image and Float'Image are attributes that convert numbers to their string representations.
  • New_Line is used to insert a newline.

  1. File I/O

File I/O in Ada is handled through the Ada.Text_IO package, which provides procedures for reading from and writing to files.

Writing to a File

with Ada.Text_IO; use Ada.Text_IO;

procedure Write_To_File is
   File : File_Type;
begin
   Create(File, Out_File, "output.txt");
   Put_Line(File, "This is a line in the file.");
   Close(File);
end Write_To_File;

Explanation:

  • Create opens a file for writing.
  • Put_Line(File, ...) writes a line to the file.
  • Close closes the file.

Reading from a File

with Ada.Text_IO; use Ada.Text_IO;

procedure Read_From_File is
   File : File_Type;
   Line : String (1 .. 100);
   Length : Natural;
begin
   Open(File, In_File, "input.txt");
   while not End_Of_File(File) loop
      Get_Line(File, Line, Length);
      Put_Line(Line(1 .. Length));
   end loop;
   Close(File);
end Read_From_File;

Explanation:

  • Open opens a file for reading.
  • Get_Line(File, Line, Length) reads a line from the file.
  • End_Of_File checks if the end of the file has been reached.

Practical Exercises

Exercise 1: Basic I/O

Task: Write a program that asks the user for their age and then prints a message saying how old they will be in 5 years.

Solution:

with Ada.Text_IO; use Ada.Text_IO;

procedure Age_In_Five_Years is
   Age : Integer;
begin
   Put("Enter your age: ");
   Get(Age);
   Put_Line("In 5 years, you will be " & Integer'Image(Age + 5) & " years old.");
end Age_In_Five_Years;

Exercise 2: File I/O

Task: Write a program that reads a list of names from a file called names.txt and prints each name to the console.

Solution:

with Ada.Text_IO; use Ada.Text_IO;

procedure Print_Names_From_File is
   File : File_Type;
   Name : String (1 .. 100);
   Length : Natural;
begin
   Open(File, In_File, "names.txt");
   while not End_Of_File(File) loop
      Get_Line(File, Name, Length);
      Put_Line(Name(1 .. Length));
   end loop;
   Close(File);
end Print_Names_From_File;

Conclusion

In this section, we covered the basics of input and output in Ada, including reading from and writing to the console, formatted I/O, and file I/O operations. These skills are essential for creating interactive programs and handling data from various sources. In the next section, we will delve into file handling in more detail, exploring advanced file operations and techniques.

© Copyright 2024. All rights reserved