comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.dash-bauhaus@futureapps.de>
Subject: Re: C getchar() functionality in Ada
Date: Tue, 17 Feb 2009 14:01:14 +0100
Date: 2009-02-17T14:01:15+01:00	[thread overview]
Message-ID: <499ab51b$0$30223$9b4e6d93@newsspool1.arcor-online.net> (raw)
In-Reply-To: <499a71f2$0$14834$4f793bc4@news.tdc.fi>

Niklas Holsti schrieb:
> Thomas Locke wrote:
>> Jeffrey R. Carter wrote:
>>
>>> This is one of the latter cases. You should not be using Character,
>>> String, or Ada.Text_IO for this. I suggest you look at streams and
>>> Ada.Text_IO.Text_Streams.
> 
> Or Ada.Sequential_IO, instantiated for Character (or Storage_Element,
> etc.) which seems to me the best match to the original C program.
> However, Sequential_IO has the problem that the standard-input and
> standard-output channels are not accessible, you must Open named files.
> 
>> I will try this when my Ada skills have improved a bit. For now I will
>> focus on learning how to best use Text_IO.
>>
>> I wouldn't mind a smallish example on using Ada.Text_IO.Text_Streams
>> though...  :D
> 
> Here is a copy-input-to-output program with Text_Streams and Character:
> 
> with Ada.Text_IO.Text_Streams;
> 
> procedure Acopy
> is
>    use Ada.Text_IO;
>    use Ada.Text_IO.Text_Streams;
> 
>    Input  : constant Stream_Access := Stream (Standard_Input);
>    Output : constant Stream_Access := Stream (Standard_Output);
> 
>    Item : Character;
> 
> begin
> 
>    while not End_Of_File (Standard_Input) loop
> 
>       Character'Read (Input , Item);
>       Character'Write(Output, Item);
> 
>    end loop;
> 
> end Acopy;


We can write the same program in Ada, of course. :-)
Only, the Ada version is likely written without
resorting to the C's idiomatic
"statement-used-as-expression-in-the-loop-conditional".

with Interfaces.C;

procedure C_1 is

   use Interfaces.C;

   function getchar return int;
   function putchar(item: int) return int;

   pragma Import(C, getchar);
   pragma Import(C, putchar);

   EOF: constant int := -1;             --  see <stdio.h>
   c, not_checked: int;

begin
   loop
      c := getchar;
      exit when C = EOF;
      not_checked := putchar(c);
   end loop;
end C_1;



  reply	other threads:[~2009-02-17 13:01 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-16 20:36 C getchar() functionality in Ada Thomas Locke
2009-02-16 21:43 ` Jeffrey R. Carter
2009-02-17  7:23   ` Thomas Locke
2009-02-17  8:16     ` Niklas Holsti
2009-02-17 13:01       ` Georg Bauhaus [this message]
2009-02-17 22:03         ` Thomas Locke
2009-03-03  8:57         ` David Thompson
2009-02-17 20:48     ` Jeffrey R. Carter
2009-02-17 21:59       ` Thomas Locke
2009-02-16 23:46 ` Adam Beneschan
2009-02-17  7:19   ` Thomas Locke
2009-02-17  8:37     ` Jacob Sparre Andersen
2009-02-17 17:31       ` Keith Thompson
2009-02-17 14:57     ` Hibou57 (Yannick Duchêne)
2009-02-17 14:55   ` Hibou57 (Yannick Duchêne)
2009-02-17 10:37 ` m.collado
2009-02-17 22:07   ` Thomas Locke
2009-02-17 16:40 ` Jean-Pierre Rosen
2009-02-17 19:46   ` Thomas Locke
2009-02-17 20:24     ` Hyman Rosen
2009-02-17 23:44       ` Robert A Duff
2009-02-18  5:44         ` anon
2009-02-18  0:39       ` Adam Beneschan
2009-02-18  7:43     ` Jean-Pierre Rosen
2009-02-18 11:42       ` christoph.grein
2009-02-17 20:38   ` Jeffrey R. Carter
2009-02-18  7:46     ` Jean-Pierre Rosen
2009-02-18 10:41       ` christoph.grein
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox