From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,fa4a527ee1c9d1e6 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!local01.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail NNTP-Posting-Date: Thu, 11 Aug 2005 12:01:16 -0500 From: tmoran@acm.org Newsgroups: comp.lang.ada Subject: Re: Using fwrite in Ada References: X-Newsreader: Tom's custom newsreader Message-ID: Date: Thu, 11 Aug 2005 12:01:16 -0500 NNTP-Posting-Host: 24.6.143.98 X-Trace: sv3-nR6VuqFd4k0QVswQLZjcNFRzkaCNQEqUeClcvPcvS26su3oEGTUDVZnBMNS2lnPVnErHsDSk4JYsBLW!pCKKC78RJ0jT91gaMQTZgRsBql14y7ZpwXaKgEDnlXCuY1aW1qDpzLJxYh+pvSjDTpuDL5FGODeO!uZGLpEOpvcfsBGFw7RPo80TI2eo= X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news1.google.com comp.lang.ada:4089 Date: 2005-08-11T12:01:16-05:00 List-Id: > 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);