โŒ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayAda โ€“ The Craft of Coding

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;


ย 

ย 

ย 

โŒ
โŒ