# Ada.Text\_IO.Text\_Streams and Ada.Text\_IO.End\_Of\_File interact oddly

**URL:** <https://forum.ada-lang.io/t/ada-text-io-text-streams-and-ada-text-io-end-of-file-interact-oddly/1765>\
**Category:** General\
**Created:** [January 30, 2025, 6:55pm UTC](https://forum.ada-lang.io/t/ada-text-io-text-streams-and-ada-text-io-end-of-file-interact-oddly/1765 "2025-01-30T18:55:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TKurtBond](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@TKurtBond](https://forum.ada-lang.io/u/TKurtBond)\
**Post date:** [January 30, 2025, 6:55pm UTC](https://forum.ada-lang.io/t/ada-text-io-text-streams-and-ada-text-io-end-of-file-interact-oddly/1765/1 "2025-01-30T18:55:28Z")

</div>

So, the following program kinda sorta looks like a naive implementation of the simplest concept of the Unix cat program:

```ada
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Streams, Ada.Text_IO.Text_Streams;

procedure Not_Really_Cat is
   Input_Stream : access Ada.Streams.Root_Stream_Type'Class :=
     Ada.Text_IO.Text_Streams.Stream (File => Ada.Text_IO.Current_Input);
   Output_Stream : access Ada.Streams.Root_Stream_Type'Class :=
     Ada.Text_IO.Text_Streams.Stream (File => Ada.Text_IO.Current_Output);

   C : Character;
begin
   while not End_Of_File loop
      Character'Read (Input_Stream, C);
      Character'Write (Output_Stream, C);
   end loop;
end Not_Really_Cat;

```

But it is not!

If you do

```ada
printf 'Hello!\n' | ./cat | od -c

```

you get

```ada
0000000 H e l l o !
0000006

```

Notice that there is no newline at the end, even though there was a newline in the input.

Looking at this a little closer, it turns out that this happens because the `End_Of_File` function in use there is from `Ada.Text_IO`, and it behaves **differently** from the `End_Of_File` function that is used on Stream files, as declared in `Ada.Streams.Stream_IO`.

Here’s another program, that uses `Ada.Streams.Stream_IO`:

```ada
with Ada.Streams, Ada.Text_IO.Text_Streams;
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
with Ada.Command_Line; use Ada.Command_Line;

procedure Try_Streams is
   Input_File : File_Type;
   Input_Stream : access Ada.Streams.Root_Stream_Type'Class;
   Output_Stream : access Ada.Streams.Root_Stream_Type'Class :=
     Ada.Text_IO.Text_Streams.Stream (File => Ada.Text_IO.Current_Output);

   C : Character;

begin
   Open (Input_File, In_File, Argument (1)); -- "../data/try_streams.dat"
   Input_Stream := Stream (Input_File);
   while not End_Of_File (Input_File) loop
      Character'Read (Input_Stream, C);
      Character'Write (Output_Stream, C);
   end loop;
end Try_Streams;

```

If I pass the name of a file that contains just `Hello!\n` to this program, it works properly and outputs the final newline.

But as the File\_Type from Ada.Text\_IO and the File\_Type from Ada.Streams.Stream\_IO are not compatible, I can’t see any way to use the End\_Of\_File from Ada.Streams.Streams\_IO with a Stream created from Ada.Text\_IO.Current\_Input by way of the function `Ada.Text_IO.Text_Streams.Stream`.

Is my understanding correct?

Is there any way to portably obtain a value of `Ada.Streams.Stream_IO.File_Type` that is associated with the standard input, so that it may be used with `Ada.Streams.Stream_IO.End_Of_File`?

---

<div class="post-metadata">

**Author:** ![dmitry-kazakov](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/dmitry-kazakov/32/522_2.png) [@dmitry-kazakov](https://forum.ada-lang.io/u/dmitry-kazakov)\
**Post date:** [January 30, 2025, 7:38pm UTC](https://forum.ada-lang.io/t/ada-text-io-text-streams-and-ada-text-io-end-of-file-interact-oddly/1765/2 "2025-01-30T19:38:11Z")

</div>

Forget that End\_Of\_File even exists. It is inefficient and guaranteed not to work in many cases.

Either use exception (End\_Error) or else stream I/O protocol:

```ada
with Ada.Streams; use Ada.Streams;
with Ada.Text_IO.Text_Streams; use Ada.Text_IO.Text_Streams;
with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
with Ada.Command_Line; use Ada.Command_Line;

procedure Main is
   Input_File : File_Type;
begin
   Open (Input_File, In_File, Argument (1));
   declare
      Input : Root_Stream_Type'Class renames Stream (Input_File).all;
      Output : Root_Stream_Type'Class renames Stream (Ada.Text_IO.Standard_Output).all;
      Buffer : Stream_Element_Array (1..120);
      Last : Stream_Element_Offset;
   begin
      loop
         Input.Read (Buffer, Last);
         exit when Last < Buffer'First;
         Output.Write (Buffer (1..Last));
      end loop;
   end;
   Close (Input_File);
   Set_Exit_Status (Success);
exception
   when others =>
      Set_Exit_Status (Failure);
end Main;

```

---

<div class="post-metadata">

**Author:** ![TKurtBond](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@TKurtBond](https://forum.ada-lang.io/u/TKurtBond)\
**Post date:** [January 30, 2025, 11:55pm UTC](https://forum.ada-lang.io/t/ada-text-io-text-streams-and-ada-text-io-end-of-file-interact-oddly/1765/3 "2025-01-30T23:55:32Z")

</div>

I was not aware that End\_Of\_File was inefficient; that’s interesting. I agree using a buffer is a much better way to do it in general.

Thanks!

---

<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:** [January 31, 2025, 8:33am UTC](https://forum.ada-lang.io/t/ada-text-io-text-streams-and-ada-text-io-end-of-file-interact-oddly/1765/4 "2025-01-31T08:33:41Z")

</div>

Streams are for heterogenous binary I/O. I don’t know why you’d use them for homogenous text I/O. To copy text from standard input to standard output:

```ada
with Ada.Text_IO;

procedure I2O is
begin
   All_Lines : loop
      Ada.Text_IO.Put_Line (Item => Ada.Text_IO.Get_Line);
   end loop All_Lines;
exception -- I2O
when Ada.Text_IO.End_Error =>
   null;
end I2O;

```

Ada.Text\_IO.End\_Of\_File has the problem that if your file ends with a null line, it returns True before you have read that last line. Handling End\_Error avoids that:

```ada
$ printf 'Hello!\n' | ./i2o | hd
00000000 48 65 6c 6c 6f 21 0a |Hello!.|
00000007
$ printf 'Hello!\n\n' | ./i2o | hd
00000000 48 65 6c 6c 6f 21 0a 0a |Hello!..|
00000008

```

Having the text input come from a file is a simple change.

---

<div class="post-metadata">

**Author:** ![dmitry-kazakov](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/dmitry-kazakov/32/522_2.png) [@dmitry-kazakov](https://forum.ada-lang.io/u/dmitry-kazakov)\
**Post date:** [January 31, 2025, 9:43am UTC](https://forum.ada-lang.io/t/ada-text-io-text-streams-and-ada-text-io-end-of-file-interact-oddly/1765/5 "2025-01-31T09:43:09Z")

</div>

> [@JC001](#):
>
> I don’t know why you’d use them for homogenous text I/O

Ada.Text\_IO translates delimiters of a text file and/or file records (in a more advanced file system, e.g. under VMS). As such it meant for text files only. In theory one should use Ada.Text\_IO. In practice, dealing with a UNIX clone, an OS that conflates and twists everything, where nothing is a rule and everything a convention, stream I/O is a safer choice because it works at the most low level possible.
