Binary files._(re)
Mon, 7 Apr 1997 17:06:36 +0200 (MET DST)
According to Berend De Schouwer:
>
> I'd like to read bytes from a file, so I looked at some of the examples.
> I found an assign(file of byte...). Cool. It doesn't seem to
> work to great tho... How big does it read? Are you sure its just 1 byte?
Yes. See below for an example program which I just tested to work
- with the alpha version gpc-970401.
> How big is a byte anyway?
8 bits. :-)
> Looking at the examples and the info file,
> I get confused. Is an integer 16bit or 32bit?
32.
> What happens with
> type byte=0..255;
It will take 4 bytes. :-( Sorry - this is a known bug.
> I tried defining them like in the info file (using
> __short__, etc.) but it doesn't compile. When I use the example's stuff
> it does compile.
See below for something which does compile.
> Hmmm... I am converting some stuff from borland, and some from fpk,
> so I would like to know how to get 8, 16 and 32 bit signed and unsinged
> integers. I don't mind doing a search/replace on the source, but I do
> need to know how big each thing is.
The alpha version gpc-970401 has the following built-in types:
Byte unsigned 8 bits
ByteInt signed 8 bits
ShortWord unsigned 16 bits
ShortInt signed 16 bits
Word unsigned 32 bits
Integer signed 32 bits
LongWord unsigned 64 bits
LongInt signed 64 bits
Single = ShortReal 32 Bits
Double = Real 64 Bits
Extended = LongReal 80 Bits on the PC, maybe 128 elsewhere
However there is one known problem with these types: You cannot
`write' or `writeln' them to text file. (Binary files are okay.)
If you
writeln ( a [ 7 ] );
with `a' being an array of bytes, you might get some very strange
result because `writeln' outputs the integer consisting of `a [ 7 ]'
and the following three bytes.
To work around, cast these types to their base type:
writeln ( Integer ( a [ 7 ] ) );
(The same holds for an `Extended' which you must cast to `Real' in
order to `writeln' it.)
> AND how to apply them properly to files ;)
I hope the example below helps you.
If you want to upgrade to gpc-970401, the URL is
ftp://agnes.dida.physik.uni-essen.de/gnu-pascal/alpha/
Greetings,
Peter
Dipl.-Phys. Peter Gerwinski, Essen, Germany, free physicist and programmer
peter.gerwinski@uni-essen.de - http://home.pages.de/~peter.gerwinski/ [970201]
maintainer GNU Pascal [970401] - http://home.pages.de/~gnu-pascal/ [970125]
8< ---- test.pas -------------------------------------------------------------
{OK} (* This program will read its own source and output 'OK'. *)
Program Test;
Type
ByteInt = __byte__ Integer;
Byte = __unsigned__ ByteInt;
ByteFile = file of Byte;
Var
F: ByteFile;
C: Char;
Procedure Assign ( Var T: ByteFile; Name: String );
Var
B: BindingType;
begin (* Assign *)
unbind ( T );
B:= binding ( T );
B.Name:= Name;
bind ( T, B );
B:= binding ( T );
end (* Assign *);
begin
Assign ( F, 'test.pas' );
reset ( F );
read ( F, C );
read ( F, C );
write ( C );
read ( F, C );
writeln ( C );
close ( F );
end.
Peter Gerwinski (peter@agnes.dida.physik.uni-essen.de)
HTML conversion by Lluís de Yzaguirre i Maura
Institut de Lingüística Aplicada -
Universitat "Pompeu Fabra"
e-mail: de_yza@upf.es