-- Skip to question farther below.
--
-- Background comments, skip to question below:
--
-- A.10 is probably the worst possibly written section of RM95 by the main author/contractor, showing that the
-- GS-15 DoD political appointee gatekeepers were too highly paid at the time even to care. Their Chief
-- Cheese whom I met and got to know at the first Albuquerque Sig Ada conference in 1993 clearly comes to mind.
-- Question, start reading here:
--
-- What is the best choice of Ada 95 procedures to perform here these gold-standard file operations of True BASIC (TB):
--
-- CLOSE #3, OPEN #3, ASK #3, SET #3, READ #3, and CLOSE #3 ?
--
-- 16
PROCEDURE Get_file_input IS --
-- All variables are defined in a separate Global_variable.adS file
-- with extensions embedded in the object name as con_str, unbounded_str, int, and con_int for
-- constant string, unbounded string, integer, and constant integer.
--
-- All arrays are defined in a separate dimension matrix Dim_mats.adB file
-- with prefixes embedded in the object file name as ar1_, ar2_, ar3_, and ar4_ to designate
-- the number of dimensions and with respective variable type extensions of _int, _unbounded_string.
BEGIN
input_file_name_unb_str := To_unbounded_string( "input_file_name.txt") ;
user_input_file_unb_str := current_usr_dir_unb_str & input_file_name_unb_str ; -- former is from startup system call
--
-- This closes a comm channel, numbered below, if open, to avoid system error opening same channel if already open.
--
CLOSE #3
--
-- This opens a closed comm channel with file name, opening file permissions as old or new file, byte stream file type,
-- and access as both in and out.
--
OPEN #3: NAME user_input_file$, CREATE NEWOLD, ORG BYTE, ACCESS OUTIN
--
-- This gets file size of file name.
--
ASK #3: FILESIZE user_input_file_size_int
IF user_input_file_size_int < 2 THEN -- checks for file size greater than CR-LF of two bytes
LET fall_through_int := 1 / 0 ; -- forces exception handled below
END IF
--
-- This sets the record size of file as the same size of file.
--
SET #3: RECSIZE user_input_file_size_int
--
-- This reads content of file into string of that same size.
--
READ #3: user_input_file_size_int, read_file_input_unb_str
--
-- This closes comm channel to have nice manners.
--
CLOSE #3 -- closes comm channel
-- - - - - - - - - - - - - - - - - - - - -
--
-- input string manipulation elided here
--
-- - - - - - - - - - - - - - - - - - - - -
EXCEPTION
WHEN Others =>
New_line ;
Put_line( " The file named " & To_string( user_input_file_unb_str)) ;
Put_line( " is empty, or it contains no semi-colon as a sentinel to the input line.") ;
New_line ;
Put_line( " Please edit the file with a correct input; the program is stopping now.") ;
New_line ;
RAISE EXCEPTION "Program is stopped." ;
END Get_file_input ; --
EDIT: Jeffrey has a much better answer below
EDIT: Thank you very much.
To determine file size in bytes in Ada 95:
type Byte is mod 256;
for Byte'Size use 8;
package BIO is new Ada.Direct_IO (Element_Type => Byte);
...
BIO.Open (File => File, Mode => BIO.In_File, Name => Name);
Size := Whatever (BIO.Size (File) );
BIO.Close (File => File);
where Whatever
is the type of Size
(BIO.Size
returns BIO.Count
).
Great, thank you.___
Isn’t this the simplest way to determine file size_count_pos in Ada 95 ?
WITH Ada.text_io; USE Ada.text_io ;
WITH Ada.direct_io; USE Ada.direct_io;
. . . .
Declare:
size_count_pos : Positive ;
Open( file_name_con_str, file_name_con_str, IN String = “”) ;
Size( file_name_con_str: IN File_type) RETURN size_count_pos ;
So long as you instantiate Direct_IO with something the size of a byte (e.g. Interfaces.Unsigned_8).
Doesn’t say so in the 2022 RM, but a little experiment shows that Size
returns the number of elements in the file.