# Request boilerplate / resusable code in Ada 95 for external file open, append, close

**URL:** <https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193>\
**Category:** General\
**Created:** [September 11, 2024, 2:32am UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193 "2024-09-11T02:32:50Z")\
**Posts on this page:** 20\
**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:** [September 11, 2024, 2:32am UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/1 "2024-09-11T02:32:50Z")

</div>

I scanned Predefined input and output in 66 pages of Cohen 2nd for an undergraduate lecture on file systems. I have Feldman, but learned not to trust it because of errors and no corrigenda.

What I want is reusable Ada 95 code to:

1. Check external file name is _not_ open, and if so then close it.
2. Open external file name as output mode only.
3. Write strings in ASCII character range of 0-127 sequentially in APPEND commands.
4. Close external file name.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [September 11, 2024, 3:04am UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/2 "2024-09-11T03:04:07Z")

</div>

Is Ada95 an absolute requirement?  
The EVIL library has [`EVIL.Util.Files`](https://github.com/OneWingedShark/EVIL/blob/8532aeb46c19e256ccc6dbc5e0452d44f1cf0965/src/evil-util-files.ads), which provides an auto-closing file-object; it’s in Ada2012, though.

---

<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:** [September 11, 2024, 3:45am UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/3 "2024-09-11T03:45:24Z")

</div>

Yes, I am afraid so, Ada 95 is an absolute requirement, but thanks for the link.

---

<div class="post-metadata">

**Author:** ![mgrojo](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/mgrojo/32/15_2.png) [@mgrojo](https://forum.ada-lang.io/u/mgrojo)\
**Post date:** [September 11, 2024, 1:23pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/4 "2024-09-11T13:23:08Z")

</div>

Check this: [Ada - Source Treasury](http://adapower.com/index.php?Command=classes&Title=Source+Treasury)

I think most, if not all, examples are in Ada95.

---

<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:** [September 11, 2024, 4:40pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/5 "2024-09-11T16:40:46Z")

</div>

Thanks for the link …

---

<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:** [September 11, 2024, 8:13pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/7 "2024-09-11T20:13:41Z")

</div>

Is this a viable coding solution to the request?

```ada
WITH Ada.text_io ; USE Ada.text_io ;

PROCEDURE Write_ASCII_characters IS
    File : File_type ;
BEGIN

    -- Step 1: Check if the external file is open and close it if necessary.
    BEGIN 
        Close( File) ; 
    EXCEPTION 
        WHEN Name_error => null ; 
        WHEN Others => null ; 
    END;

    -- Step 2: Open external file as output mode only.
    Create( File, Out_file, "filename.txt") ;

    -- Step 3: Write strings in ASCII character range of 0-127 sequentially.
    Open ( File, Append_file, "filename.txt") ;
    
    FOR i IN 0 .. 127 LOOP 
        Put_line( File, Character'VAL( i)'Image); 
    END LOOP ;

    -- Step 4: Close external file name.
    Close( File) ;

END Write_ASCII_characters ;

```

---

<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:** [September 12, 2024, 12:20pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/8 "2024-09-12T12:20:05Z")

</div>

I suggest you at least try to compile your code before posting. All of your questions are answered by the [standard library documentation](https://www.adaic.org/resources/add_content/standards/95lrm/ARM_HTML/RM-A.html), which anyone using Ada should have read and understood.

---

<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:** [September 12, 2024, 2:09pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/9 "2024-09-12T14:09:04Z")

</div>

The standard library documentation, as you know, is for vendors building Ada compilers, hence the name of _standard_ ref. man. The response of “read the manual” really does not apply here because my question, per the subject line, refers to reusable code from practitioners. Compiling the instant code is not possible because the GNAT version obtained is broken (please see my other posts), and no replacement is suggested here.

May I also request not minimizing my question to dismiss it, as I am still an Ada 95 newbie.

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [September 12, 2024, 8:56pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/10 "2024-09-12T20:56:46Z")

</div>

> [@m8vl4](#):
>
> The standard library documentation, as you know, is for vendors building Ada compilers, hence the name of _standard_ ref. man.

Well, yes; but also it lets you see what is bundled in the standard library.  
Checking it out is absolutely germane to the question; especially as it can show how difficult it would be to (e.g.) refactor a solution like I presented to use the older standard.

> [@m8vl4](#):
>
> The response of “read the manual” really does not apply here because my question, per the subject line, refers to reusable code from practitioners.

I _think_ that all the main stuff `EVIL` uses was doable w/ Ada95; if so, then it’s just a matter of converting the Ada2005 dot-notation to subprogram-call notation.

> [@m8vl4](#):
>
> Compiling the instant code is not possible because the GNAT version obtained is broken (please see my other posts), and no replacement is suggested here.

Every time I hear about GNAT bootstrapping problems I wonder _why_ it should be so problematic to bootstrap — I don’t necessarily think it is the complexity of the language, despite some others’ posts, but rather it seems to be GNAT/GCC’s own architecture getting in its own way.

---

<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:** [September 12, 2024, 10:19pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/11 "2024-09-12T22:19:05Z")

</div>

> [@Request boilerplate / resusable code in Ada 95 for external file open, append, close](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/9):
>
> The standard library documentation, as you know, is for vendors building Ada compilers, hence the name of standard ref. man. The response of “read the manual” really does not apply here because my question, per the subject line, refers to reusable code from practitioners. Compiling the instant code is not possible because the GNAT version obtained is broken (please see my other posts), and no replacement is suggested here. May I also request not minimizing my question to dismiss it, as I am s…

The standard library is removed from confecting and/or reusing code which is a programming skill as supposedly taught by Ada 95 textbooks. For example in Feldman et al 3rd ed, file i/o is _absent_ except in passing.

Converting EVIL in Ada2005 to Ada1995 from its acceptable dot notation is a good idea, but in light of the 17-lines of code fragment proffered such an exercise is unnecessary on at least ten-times that size in a PRAGMA library.

In my opinion, the historical enormity of gnat/gcc, as financed by the Gov’t hook-line-and-sinker and making millionaires in the process, traces its root cause back to the mistaken dictum of Richard Stallman not to build libraries but to build file structures. Welcome to the world of C.

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [September 12, 2024, 10:46pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/12 "2024-09-12T22:46:03Z")

</div>

> [@m8vl4](#):
>
> Welcome to the world of C.

Ah, yes.  
The world where we throw away machine checkable constraints and “hope” that the programmer has the wherewithal, time, and energy to manually add them in.

Yay.  
[/sarc]

---

<div class="post-metadata">

**Author:** ![zertovitch](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zertovitch/32/90_2.png) [@zertovitch](https://forum.ada-lang.io/u/zertovitch)\
**Post date:** [September 13, 2024, 2:27am UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/13 "2024-09-13T02:27:57Z")

</div>

You are on a good way.  
Step 1 does nothing, since `File` is closed [1] and hasn’t even a name.  
At step 3 you try to open again the file opened at step 2, so you always get an error. For instance with GNAT it looks like:  
`raised ADA.IO_EXCEPTIONS.STATUS_ERROR : System.File_IO.Open: file already open`

* * *

[1] RM A.7 (5)  
“Before input or output operations can be performed on a file, the file first has to be associated with an external file. While such an association is in effect, the file is said to be open, and otherwise the file is said to be closed.”

---

<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:** [September 13, 2024, 2:53am UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/14 "2024-09-13T02:53:12Z")

</div>

…  
Step 1 does nothing, since `File` is closed [1] and hasn’t even a name.

> Did you see the code fragment proffered in this thread?

---

<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:** [September 13, 2024, 10:02am UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/15 "2024-09-13T10:02:14Z")

</div>

> [@m8vl4](#):
>
> The standard library documentation, as you know, is for vendors building Ada compilers

No, Annex A is written for users of the standard library, and easy to understand.

> [@m8vl4](#):
>
> The response of “read the manual” really does not apply here because my question, per the subject line, refers to reusable code from practitioners.

Your subject line says nothing about “reusable code from practitioners”, nor does your initial post.

> [@m8vl4](#):
>
> the GNAT version obtained is broken

Anyone can easily obtain a recent version of FSF GNAT for common platforms and use it with the `--gnat95` switch. I would suggest avoiding GNAT 13 \< 13.3 and GNAT 14 \< 14.1, as they had some significant errors, but 12.3 or 14.1 should work fine for 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:** [September 13, 2024, 12:53pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/16 "2024-09-13T12:53:31Z")

</div>

Thanks for comments.

---

<div class="post-metadata">

**Author:** ![zertovitch](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zertovitch/32/90_2.png) [@zertovitch](https://forum.ada-lang.io/u/zertovitch)\
**Post date:** [September 13, 2024, 5:07pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/17 "2024-09-13T17:07:33Z")

</div>

> [@m8vl4](#):
>
> Did you see the code fragment proffered in this thread?

Absolutely. My answer refers to that code fragment. You can track the parent post (if any) on the top right of each post (see the red arrow on the following screenshot).

![grumpy](https://forum.ada-lang.io/uploads/default/original/1X/f18408832f24d501a46233eece2ce4d198ccb2b1.png)

---

<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:** [September 13, 2024, 8:22pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/18 "2024-09-13T20:22:34Z")

</div>

Step 1 derives from the early practice in Dartmouth BASIC (1965) of closing a channel before opening it to make sure the open does not raise an exception, because closing a closed channel raises no exception.  
This in effect is mirrored in Cohen 2nd ed pg 746 with File named as file\_type [assumed here]:  
BEGIN  
Open( File, Out\_file, Name, Form) ;  
EXCEPTION  
WHEN Name\_error =\> Create( File, Out\_file, Name, Form) ;  
END ;

The Step 3 I think is to comply with the Ada 95 order of open internal file then create external file to write the 'IMAGE in the loop (which apparently goes to the screen). The idea is to print to mutable screen and to immutable file.

If this code is mistaken, then how to fix it please, the implied purpose of the request.

---

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [September 13, 2024, 9:04pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/19 "2024-09-13T21:04:10Z")

</div>

> [@m8vl4](#):
>
> Step 1 derives from the early practice in Dartmouth BASIC (1965) of closing a channel before opening it to make sure the open does not raise an exception, because closing a closed channel raises no exception.

Perhaps that’s the Dartmouth BASIC point of view, but in Ada, closing a file that’s not open raises an exception:

```ada
raised ADA.IO_EXCEPTIONS.STATUS_ERROR : System.File_IO.Check_File_Open: file not open
0x4035e1 Test_Close at test_close.adb:9

```

here’s the source file, for reference

```ada
with Ada.Text_IO;
procedure Test_Close is
   package IO renames Ada.Text_IO;
   File : IO.File_Type;
begin
   IO.Create (File, IO.Out_File, "test.txt");
   IO.Put_Line (File, "test");
   IO.Close (File);
   IO.Close (File);
end Test_Close;

```

---

<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:** [September 13, 2024, 9:45pm UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/20 "2024-09-13T21:45:50Z")

</div>

In Ada, closing a file that’s not open raises an exception: that is correct.  
Hence:  
BEGIN  
Close( File) ;  
EXCEPTION  
WHEN Name\_error =\> null ;  
WHEN Others =\> null ;  
END;

---

<div class="post-metadata">

**Author:** ![zertovitch](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zertovitch/32/90_2.png) [@zertovitch](https://forum.ada-lang.io/u/zertovitch)\
**Post date:** [September 14, 2024, 12:21am UTC](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193/21 "2024-09-14T00:21:53Z")

</div>

Modifying @cantanima’s example a little bit:

```ada
with Ada.Text_IO;

procedure Test_Close is
   package IO renames Ada.Text_IO;
   File : IO.File_Type;
begin
   IO.Close (File);
exception
   when IO.Status_Error =>
      IO.Put ("Closing a file that's not open.");
      raise;
end Test_Close;

```

You get always:

```ada
Closing a file that's not open.

raised ADA.IO_EXCEPTIONS.STATUS_ERROR : System.File_IO.Check_File_Open: file not open

```

So you can basically (pun not intended 😉 ) remove the “Step 1” of your fragment:

```ada
    -- Step 1: Check if the external file is open and close it if necessary.
    BEGIN 
        Close( File) ; 
    EXCEPTION 
        WHEN Name_error => null ; 
        WHEN Others => null ; -- ***
    END;

```

because the point maked with \*\*\* is always reached.

[Next page](https://forum.ada-lang.io/t/request-boilerplate-resusable-code-in-ada-95-for-external-file-open-append-close/1193.md?page=2)
