From: Erik Sigra <sigra@home.se>
Subject: Re: Another problem with stream reading.
Date: Wed, 27 Mar 2002 16:53:10 +0100
Date: 2002-03-27T16:53:10+01:00 [thread overview]
Message-ID: <mailman.1017244142.7106.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: <3CA0B5C4.2060804@home.com>
tisdagen den 26 mars 2002 18.54 skrev du:
> Just shooting from the hip here, I think the problem is that
> you gave the compiler an impossible assignment. By that, I
> mean that you've declared type Byte to have integer range
> 0..255 and at the same time said its size has to be 8 bits.
> There is no room for that range and a sign bit (since it is
> integer here) to exist in 8 bits (you need 9). As a result
> of lying to HAL, HAL decided to disregard your statement
> "for Byte'Size use 8" and used 16 instead.
If you look carefully at my code you can se that i did NOT write "Integer
range", just "range". And there is certainly no need for a sign bit for the
range 0 .. 255. If I lie to the compiler, it does certainly not disregard it.
It complais loudly. If I for example say "for Byte'Size use 7;", it replies
"size for "Byte" too small, minimum allowed is 8"
> If you want signed numbers, you need to fix the range. If
> you want unsigned numbers, then you need a modulo type.
I should not need a modulo type for unsigned numbers. See "Programming in Ada
95" by John Barnes, 2nd edition, page 552:
" For example we can specify the amount of storage to be allocated for
objects of a type, for the storage pool of an access type and for the working
storage of a task type. This is done by an attribute definition clause. Thus
type Byte is range 0 .. 255;
for Byte'Size use 8;
indicates that objects of type Byte should occupy only 8 bits. The size of
individual objects can also be specified."
> Erik Sigra wrote:
> > Now I have the program
> >
> > with Ada; use Ada;
> > with Text_IO; use Text_IO;
> > with Ada.Streams; use Streams;
> > with Ada.Streams.Stream_IO; use Stream_IO;
> >
> > procedure Streamtest is
> > The_File : Stream_IO.File_Type;
> > begin
> > Open (The_File, In_File, "data");
> > declare
> > The_Stream : Stream_Access := Stream (The_File);
> > type Byte is range 0 .. 255;
> > for Byte'Size use 8;
> > B : Byte;
> > begin
> > while not End_Of_File (The_File) loop
> > Byte'Read (The_Stream, B);
> > Put_Line ("Read B = " & B'Img);
> > end loop;
> > end;
> > end Streamtest;
> >
> >
> > The data file contains
> > "����" (hexadecimal "ff fe fd fc") (decimal "255 254 253 252). The output
> > of the program is:
> > Read B = 255
> > Read B = 253
> >
> >
> > The problem is that it reads 2 bytes instead of 1 and thus skips each
> > second byte. Why?
next prev parent reply other threads:[~2002-03-27 15:53 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-03-25 18:53 Another problem with stream reading Erik Sigra
2002-03-25 19:01 ` Dan Andreatta
2002-03-25 19:14 ` Erik Sigra
2002-03-25 22:20 ` Jeffrey Carter
2002-03-25 22:28 ` Stephen Leake
2002-03-26 17:54 ` Warren W. Gay VE3WWG
2002-03-27 15:53 ` Erik Sigra [this message]
2002-03-27 21:22 ` Warren W. Gay VE3WWG
2002-03-27 22:50 ` Dan Andreatta
2002-03-27 23:55 ` Randy Brukardt
2002-03-28 0:18 ` David Bolen
2002-03-28 22:30 ` Randy Brukardt
2002-03-28 0:33 ` tmoran
2002-03-28 15:21 ` Marin David Condic
2002-03-29 11:30 ` Larry Kilgallen
2002-03-29 14:33 ` Marin David Condic
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox