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:
- Basic I/O Operations
- Formatted I/O
- File I/O
- 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_IOis the package that provides basic I/O functionalities.Get_Linereads a line of text from the standard input.PutandPut_Lineare 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:
Putwrites text without a newline at the end.Put_Linewrites text followed by a newline.
- 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'ImageandFloat'Imageare attributes that convert numbers to their string representations.New_Lineis used to insert a newline.
- 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:
Createopens a file for writing.Put_Line(File, ...)writes a line to the file.Closecloses 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:
Openopens a file for reading.Get_Line(File, Line, Length)reads a line from the file.End_Of_Filechecks 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.
Ada Programming Course
Module 1: Introduction to Ada
Module 2: Basic Concepts
- Variables and Data Types
- Operators and Expressions
- Control Structures
- Loops in Ada
- Subprograms: Procedures and Functions
Module 3: Advanced Data Types
Module 4: Modular Programming
Module 5: Concurrency and Real-Time Programming
Module 6: Advanced Topics
Module 7: Best Practices and Optimization
- Code Style and Best Practices
- Debugging and Testing
- Performance Optimization
- Security Considerations
