From: "Eric G. Miller" <egm2@jps-nospam.net>
Subject: Re: Read booleans from streams
Date: Mon, 25 Mar 2002 03:57:56 GMT
Date: 2002-03-25T03:57:56+00:00 [thread overview]
Message-ID: <pan.2002.03.24.19.58.38.934664.18394@jps-nospam.net> (raw)
In-Reply-To: mailman.1017011702.18089.comp.lang.ada@ada.eu.org
In <mailman.1017011702.18089.comp.lang.ada@ada.eu.org>, Erik Sigra wrote:
> I tried to read bytes from a stream and interpret each bit as a Boolean. But
> it did not work as expected. It reads 1 byte for each bit. How should I do
> instead?
>
>
> streamtest.adb
> ==============
> with Ada.Text_IO; use Ada.Text_IO;
> with Ada.Streams; use Ada.Streams;
> with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
>
> procedure Streamtest is
> type Boolean_Aggregate is array (0 .. 7) of Boolean;
> for Boolean_Aggregate'Component_Size use 1;
>
> Current_Byte : Boolean_Aggregate;
>
> The_File : Stream_IO.File_Type;
>
> The_Stream : Stream_Access;
>
> begin
>
> Open (The_File, In_File, "data");
>
> The_Stream := Stream (The_File);
>
> loop
> Boolean_Aggregate'Read (The_Stream, Current_Byte);
> for I in Boolean_Aggregate'Range loop
> Put(Current_Byte (I)'Img & ' ');
> end loop;
> New_Line;
> end loop;
> end Streamtest;
>
>
> data
> ====
> 02027203
>
>
> user > ./streamtest
> FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
>
> raised ADA.IO_EXCEPTIONS.END_ERROR : s-stratt.adb:170
>
>
> I have checked that the size of Boolean_Aggregate is 8 and not 64.
I think the problem is in the read procedure. It sees an array of 8 elements,
so wants to read 8 bytes. I think you need a 'Read wrapper to read a single
byte (type Byte is mod 2**8), and then do an Unchecked_Conversion:
function To_Boolean_Aggregate is new
Ada.Unchecked_Conversion(Byte,Boolean_Aggregate);
procedure Read_Boolean_Aggregate
(Stream : Stream_Access; Data : in out Boolean_Aggregate) is
Item : Byte;
begin
Byte'Read (Stream, Item);
Data := To_Boolean_Aggregate (Item);
end Read_Boolean_Aggregate;
...
while not End_Of_File (File_Type) loop
Read_Boolean_Aggregate (Stream, Data);
...
end loop;
next prev parent reply other threads:[~2002-03-25 3:57 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-03-24 23:18 Read booleans from streams Erik Sigra
2002-03-25 3:57 ` Eric G. Miller [this message]
2002-03-25 11:21 ` Erik Sigra
[not found] ` <200203251121.g2PBL9401018@a77.ib.student.liu.se>
2002-03-25 16:00 ` sk
[not found] <200203242318.g2ONIkc12876@a77.ib.student.liu.se>
2002-03-25 2:05 ` sk
[not found] ` <3C9E85F6.8EDF3AAD@myob.com>
2002-03-25 10:46 ` Erik Sigra
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox