File handling is an essential aspect of programming, allowing programs to read from and write to files. In Ada, file handling is robust and provides various features to manage files efficiently. This section will cover the basics of file handling in Ada, including reading from and writing to text files, handling binary files, and using file attributes.

Key Concepts

  1. File Types: Ada supports different file types, such as text files and binary files.
  2. File Operations: Common operations include opening, reading, writing, and closing files.
  3. File Modes: Files can be opened in different modes, such as read, write, and append.
  4. File Attributes: Attributes provide information about files, such as size and modification date.

File Types

Text Files

Text files store data in a human-readable format. Each line in a text file is a sequence of characters terminated by a newline character.

Binary Files

Binary files store data in a binary format, which is not human-readable. They are used for storing data such as images, audio, and compiled programs.

File Operations

Opening a File

To open a file in Ada, you use the Open procedure from the Ada.Text_IO package for text files or Ada.Streams.Stream_IO for binary files.

with Ada.Text_IO; use Ada.Text_IO;

procedure Open_File is
   File : File_Type;
begin
   Open(File, In_File, "example.txt");
   -- Perform file operations
   Close(File);
end Open_File;

Reading from a File

To read from a text file, you use the Get_Line procedure.

with Ada.Text_IO; use Ada.Text_IO;

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

Writing to a File

To write to a text file, you use the Put_Line procedure.

with Ada.Text_IO; use Ada.Text_IO;

procedure Write_File is
   File : File_Type;
begin
   Open(File, Out_File, "example.txt");
   Put_Line(File, "Hello, Ada!");
   Close(File);
end Write_File;

Closing a File

Always close a file after performing operations to ensure data integrity.

Close(File);

File Modes

Mode Description
In_File Open file for reading
Out_File Open file for writing
Append_File Open file for appending

File Attributes

File attributes provide additional information about files. For example, you can get the size of a file using the Size attribute.

with Ada.Text_IO; use Ada.Text_IO;

procedure File_Attributes is
   File : File_Type;
   Size : Integer;
begin
   Open(File, In_File, "example.txt");
   Size := Size(File);
   Put_Line("File size: " & Integer'Image(Size));
   Close(File);
end File_Attributes;

Practical Exercises

Exercise 1: Reading a File

Task: Write a program that reads a file named input.txt and prints its contents to the console.

Solution:

with Ada.Text_IO; use Ada.Text_IO;

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

Exercise 2: Writing to a File

Task: Write a program that writes the string "Ada Programming" to a file named output.txt.

Solution:

with Ada.Text_IO; use Ada.Text_IO;

procedure Write_Output_File is
   File : File_Type;
begin
   Open(File, Out_File, "output.txt");
   Put_Line(File, "Ada Programming");
   Close(File);
end Write_Output_File;

Exercise 3: Appending to a File

Task: Write a program that appends the string "Learning Ada" to a file named log.txt.

Solution:

with Ada.Text_IO; use Ada.Text_IO;

procedure Append_Log_File is
   File : File_Type;
begin
   Open(File, Append_File, "log.txt");
   Put_Line(File, "Learning Ada");
   Close(File);
end Append_Log_File;

Common Mistakes and Tips

  • Forgetting to Close Files: Always ensure you close files after operations to prevent data corruption.
  • Incorrect File Modes: Ensure you open files in the correct mode (read, write, append) based on the operation you intend to perform.
  • Handling End of File: Use End_Of_File to check if the end of the file has been reached to avoid reading errors.

Conclusion

In this section, we covered the basics of file handling in Ada, including opening, reading, writing, and closing files. We also discussed different file modes and attributes. By practicing the provided exercises, you should now be comfortable with basic file operations in Ada. In the next section, we will explore exception handling to manage errors effectively in your programs.

© Copyright 2024. All rights reserved