โŒ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayNews from the Ada programming language world

Coding Ada: reading lines from files.

By: spqr
4 April 2018 at 22:03

How to read lines from a text file in Ada?

First create some variables for the filename, string to hold the filename, a string to hold the line of text, and a boolean value to hold the file status (nameOK):

infp : file_type;
sline : unbounded_string;
fname : unbounded_string;
nameOK : boolean := false;

Then read in the filename, continuously if it does not exist, or there are other issues with it. When this is okay, open the file for reading, and loop through the file, using the function get_line() to read a line of text, and store it in the unbounded_string sline. This can then be further processed, before a new line of text read.

put_line ("Enter the filename: ");
loop
   exit when nameOK;
   get_line(fname);
   nameOK := exists(to_string(fname));
end loop;

open(infp, in_file, to_string(fname));

loop
   exit when end_of_file(infp);
   -- Process each line from the file
   get_line(infp,sline);
   -- do something with the line of text
end loop;


ย 

ย 

ย 

Easy reading and writing files with Ada Utility Library

9 August 2020 at 20:49

The Ada Utility Library provides several simple operations that simplify the reading and writing of files through a single procedure call. These operations are not new since I've implemented most of them 10 years ago!!!

Reading a file line by line by using Ada.Text_IO is quite easy but annoying due to the fact that you have to open the file, iterate over the content getting one line at a time and then closing the file. To simplify this process, the Util.Files package of Ada Utility Library provides a simple Read_File procedure that uses a procedure as parameter and that procedure will be called for each line that is read.

with Util.Files;

  procedure Read_Line (Line : in String) is ...;

  Util.Files.Read_File (Path => "file.txt",
                        Process => Read_Line'Access);

Another Read_File procedure allows to read the file and get its content in a vector of strings so that once the file is read the vector contains each line. Yet, another Read_File procedure reads the file content in an Unbounded_String. You will use that later form as follows:

with Util.Files;
with Ada.Strings.Unbounded;

  Content : Ada.Strings.Unbounded.Unbounded_String;

  Util.Files.Read_File (Path => "file.txt",
                        Into => Content);

Very often it is also necessary to write some content in a file. Again, this is easy to do but a simple Write_File procedure that takes the file path and the content to write is easier to use in several cases:

with Util.Files;

  Util.Files.Write_File (Path => "file.txt",
                         Content => "something");

The Ada Utility Library contains other useful operations and features that have helped me in implementing various projects such as Ada Web Application and Ada Keystore. Have a look at the Ada Utility Library Programmer's Guide!

โŒ
โŒ