comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@acm.org
Subject: Re: Using fwrite in Ada
Date: Thu, 11 Aug 2005 12:01:16 -0500
Date: 2005-08-11T12:01:16-05:00	[thread overview]
Message-ID: <caGdnZ2dnZ30zOuZnZ2dncEZZt-dnZ2dRVn-y52dnZ0@comcast.com> (raw)
In-Reply-To: dddbut$7qm$1@newsm1.svr.pol.co.uk

> I want to read and write binary data from stdin/stdout, unfortunately Ada's
> usual IO routines appear to be text only and (on Win32) convert code 10 to
> codes 10 & 13.
  Ada.Text_IO.Text_Streams addresses exactly that problem.

> To get round this I'm trying to use Interfaces.C_Streams, but I don't know
> How to I get a String into the fwrite function?
   Do you need to call "fwrite", or do you just need to write binary data
that can be read back by another Ada program?  In the latter case, 'write,
as has been suggested, will do the job.  The bit pattern that gets written
is implementation defined, unless you override that.  That can be handy.

>  cptr : chars_ptr;
>  Buffer : String (1..100);
>   cptr:=New_String(Buffer);
  implies that chars_ptr is an access to an unconstrained array.  So it
points to both the subscript range of the string and to the character data
in the string.  That is not the same thing as a C pointer to a byte (that
may happen to be the first byte in the string).

>   vptr:=voids(cptr);
>
>   fwrite(vptr,100,1,stdout);
> But this won't compile, the compiler says that it cannot convert the
> chars_ptr to voids. Coming from a C background, I don't understand why
> something cannot be cast to void.
   In Ada, type matters.  What would you expect to happen if you said
  cptr := chars_ptr(vptr);
How would the cptr find out the size of the string that held the byte
that vptr currently points to?

  Look further at Interfaces.  You seem to want to be able to call fwrite
with a String parameter and ignore its result:
   Tell the compiler:
   procedure fwrite(Source : String;
                   Size, Length : Natural;
                   Target : File_Ptr);
   pragma Import(fwrite, C);
then use it
   fwrite(Buffer, 1, Buffer'length, stdout);



  parent reply	other threads:[~2005-08-11 17:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-10 17:01 Using fwrite in Ada Makhno
2005-08-10 17:28 ` Georg Bauhaus
2005-08-10 18:05 ` Martin Krischik
2005-08-11 17:01 ` tmoran [this message]
2005-08-11 21:59   ` Jeffrey Carter
replies disabled

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