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,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8f01d35116e753b6 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.235.4 with SMTP id ui4mr23781778pbc.3.1332875478466; Tue, 27 Mar 2012 12:11:18 -0700 (PDT) Path: z9ni8525pbe.0!nntp.google.com!news1.google.com!news4.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Michael Moeller Newsgroups: comp.lang.ada Subject: Re: xor Date: Tue, 27 Mar 2012 21:09:02 +0100 Message-ID: References: <9t8mq9Fla4U1@mid.individual.net> <9t99r9F6e2U1@mid.individual.net> Mime-Version: 1.0 X-Trace: individual.net I1CDcXHnROkMaVvAOg4pwQTlxN5vUPVk9eBboKRJepcuhq7Nq9 X-Orig-Path: kodiak1!mic2 Cancel-Lock: sha1:8CMR4ZbZbEiNuUVdUEizaO7GBF0= X-X-Sender: mic2@kodiak1 In-Reply-To: <9t99r9F6e2U1@mid.individual.net> Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Date: 2012-03-27T21:09:02+01:00 List-Id: On Sun, 25 Mar 2012, Niklas Holsti wrote: > On 12-03-25 18:16 , Michael Moeller wrote: >> Thank you for your quick answer. >> >> Can I use Unsigned_Byte_T to read in a byte from a file >> as in C? I haven't looked into this yet. > > C has several ways to "read in a byte from a file", depending on the type of > the file and how you specify which byte you want to read. Can you be more > specific? I have to read in what in C is called an unsigned byte, which in fact is a stream of bytes without any interpretation as a symbol. Just a number from 0 to 255. In addition it would be nice to call something like 'fstat' to get the size of file in advance instead of checking every byte for EOF. > > If you mean reading an 8-bit octet from a "binary file" -- that is, not a > text file -- you can use the Ada library packages Ada.Sequential_IO or > Ada.Direct_IO, depending on your needs. These are generic packages, so you > must instantiate them for the type of data, for example Unsigned_Byte_T. > > If the file contains a mixture of different kinds of "binary" data, for > example both 8-bit data and 16-bit data, or more complex structures, you > should use the Ada "stream" facility from Ada.Streams.Stream_IO. The stream > facility helps you handle things like different endianness, padding, packing, > unusual encodings, and so on. > > The stream facility does not use generics; instead, it uses a combination of > tagged types and the special "stream attribute" operations T'Read, T'Write, > et cetera, which you can define differently for each type, T, that is present > in your file. For example, your 'Read operation for a 16-bit integer will > specify whether the integer is stored in little-endian or big-endian order in > the file (by reading two 8-bit integers, using 'Read for the 8-bit type, in > the corresponding order). > > The stream technique takes a bit of study to understand, but it works very > well, especially if the file has a mixture of different complex structures of > data, or if the file is created by a different language or computer and is > not in the "native" form for your Ada compiler. > > -- > Niklas Holsti > Tidorum Ltd > niklas holsti tidorum fi > . @ . > Perhaps it will be more save to leave the C-procedures alone and to call them from within Ada instead. I don't know whether this will be easier. However, I'll give Sequential_IO and Direct_IO a try. Regards, Michael