# In Ada 95, which I/O procedures to use for these TB commands, please

**URL:** https://forum.ada-lang.io/t/in-ada-95-which-i-o-procedures-to-use-for-these-tb-commands-please/1026
**Category:** General
**Created:** [July 27, 2024, 9:14pm UTC](https://forum.ada-lang.io/t/in-ada-95-which-i-o-procedures-to-use-for-these-tb-commands-please/1026 "2024-07-27T21:14:04Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![m8vl4](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@m8vl4](https://forum.ada-lang.io/u/m8vl4)
#### Post date: [July 27, 2024, 9:14pm UTC](https://forum.ada-lang.io/t/in-ada-95-which-i-o-procedures-to-use-for-these-tb-commands-please/1026/1 "2024-07-27T21:14:04Z")

</div>

```ada
-- 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 ; --

```

---

<div class="post-metadata">

### Author: ![jere](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jere/32/87_2.png) [@jere](https://forum.ada-lang.io/u/jere)
#### Post date: [July 28, 2024, 6:23am UTC](https://forum.ada-lang.io/t/in-ada-95-which-i-o-procedures-to-use-for-these-tb-commands-please/1026/4 "2024-07-28T06:23:44Z")

</div>

EDIT: Jeffrey has a much better answer below

---

<div class="post-metadata">

### Author: ![m8vl4](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@m8vl4](https://forum.ada-lang.io/u/m8vl4)
#### Post date: [July 28, 2024, 10:10am UTC](https://forum.ada-lang.io/t/in-ada-95-which-i-o-procedures-to-use-for-these-tb-commands-please/1026/5 "2024-07-28T10:10:01Z")

</div>

EDIT: Thank you very much.

---

<div class="post-metadata">

### Author: ![JC001](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@JC001](https://forum.ada-lang.io/u/JC001)
#### Post date: [July 28, 2024, 11:08am UTC](https://forum.ada-lang.io/t/in-ada-95-which-i-o-procedures-to-use-for-these-tb-commands-please/1026/6 "2024-07-28T11:08:56Z")

</div>

> [@m8vl4](#):
>
> apparently Ada 95 does not have have a buffer size derived from file size

To determine file size in bytes in Ada 95:

```ada
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`).

---

<div class="post-metadata">

### Author: ![m8vl4](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@m8vl4](https://forum.ada-lang.io/u/m8vl4)
#### Post date: [July 28, 2024, 12:52pm UTC](https://forum.ada-lang.io/t/in-ada-95-which-i-o-procedures-to-use-for-these-tb-commands-please/1026/7 "2024-07-28T12:52:39Z")

</div>

Great, thank you.\_\_\_

---

<div class="post-metadata">

### Author: ![m8vl4](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@m8vl4](https://forum.ada-lang.io/u/m8vl4)
#### Post date: [August 1, 2024, 11:25pm UTC](https://forum.ada-lang.io/t/in-ada-95-which-i-o-procedures-to-use-for-these-tb-commands-please/1026/9 "2024-08-01T23:25:24Z")

</div>

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 ;

---

<div class="post-metadata">

### Author: ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)
#### Post date: [August 2, 2024, 7:32am UTC](https://forum.ada-lang.io/t/in-ada-95-which-i-o-procedures-to-use-for-these-tb-commands-please/1026/10 "2024-08-02T07:32:21Z")

</div>

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.
