Reading binary files in pascal 
Author Message
 Reading binary files in pascal

Is there a way to specify how many bytes at a time must be read when
reading a binary file in Pascal.
I can for example read 2 bytes if the file is declared as "file of
integer" or 1 byte if it is declared as "file of char" and so on, but
I need a more flexible way that permits me to read any number of
bytes.
Thank you for your attention.

Davide



Sat, 01 Apr 2000 03:00:00 GMT  
 Reading binary files in pascal

Quote:
> Is there a way to specify how many bytes at a time must be read when
> reading a binary file in pascal.
> I can for example read 2 bytes if the file is declared as "file of
> integer" or 1 byte if it is declared as "file of char" and so on, but
> I need a more flexible way that permits me to read any number of
> bytes.

   Are you using Turbo Pascal or Borland Pascal?  (If so, you're not in
the proper newsgroup).  However, if you are using either TP or BP, you
can do what you want with untyped files and BlockRead/BlockWrite.


Sat, 01 Apr 2000 03:00:00 GMT  
 Reading binary files in pascal



Quote:
> Is there a way to specify how many bytes at a time must be read when
> reading a binary file in pascal.
> I can for example read 2 bytes if the file is declared as "file of
> integer" or 1 byte if it is declared as "file of char" and so on, but
> I need a more flexible way that permits me to read any number of
> bytes.
> Thank you for your attention.

Davide,

Have you looked at BlockRead and Blockwrite?  The following Delphi
fragment was used to patch an .EXE file.  The example shows the
use of both of these function to read/write a binary file:

...
  CONST
    BlockCount = 100;   {100 512-byte blocks in buffer}
    BlockSize  = 512;
    Sentinel   = $0B0bD01e;  {A politician most could trust -- a good
sentinel}

  TYPE
    TByteBuffer    = ARRAY[0..BlockCount*BlockSize-1]         OF BYTE;
{100 512-byte blocks}
    TIntegerBuffer = ARRAY[0..BlockCount*BlockSize DIV SizeOf(INTEGER) - 1]
OF INTEGER;

...                                                                        

procedure TForm1.ButtonPatchEXEClick(Sender: TObject);
   VAR
    BytesRead    :  INTEGER;
    error1       :  INTEGER;
    error2       :  INTEGER;
    FromFile     :  FILE;
    i            :  INTEGER;
    IntegerBuffer:  ^TIntegerBuffer;
    IOBuffer     :  ^TByteBuffer;
    ReplaceFlag  :  BOOLEAN;
    SentinelCount:  INTEGER;
    ToFile       :  FILE;
begin
  FileMode := 0;  {Turbo default is 2 for R/W; 0 is for R/O}
  AssignFile (FromFile, Paramstr(0));
  {$I-} RESET (FromFile,1); {$I+}
  error1 := IOResult;

  AssignFile (ToFile, 'Patched.EXE');    {Hardwire for now}
  {$I-} REWRITE (ToFile, 1); {$I+}
  error2 := IOResult;

  IF   (error1 = 0) AND (error2 = 0)
  THEN BEGIN
    ReplaceFlag := FALSE;

    SentinelValue := StrToInt(EditSentinel.Text);
    New (IOBuffer);      {Allocate the Buffer}
    SentinelCount := 0;
    REPEAT
      BlockRead (FromFile, IOBuffer^, SizeOf(TByteBuffer), BytesRead);

      {Look at buffer and if sentinel found, replace next integer with
       CRC-32}
      IntegerBuffer := Addr(IOBuffer^);
      FOR i := 0 TO BytesRead DIV 4 - 1 DO
      BEGIN
        IF   ReplaceFlag
        THEN BEGIN
          IntegerBuffer^[i] := CRC32;
          ReplaceFlag := FALSE;
          INC (SentinelCount)
        END
        ELSE ReplaceFlag := (IntegerBuffer^[i] = CRCCheckArray[1])
      END;

      BlockWrite (ToFile, IOBuffer^, BytesRead)

    UNTIL BytesRead = 0;
    CloseFile (FromFile);
    CloseFile (ToFile);
    Dispose (IOBuffer);  {Free the Buffer}
  END;

  IF   SentinelCount = 1
  THEN LabelPatchInfo.Caption := 'One Integer Patched'
  ELSE LabelPatchInfo.Caption := 'CAUTION:  Integers Patched = ' +
                                 IntToStr(SentinelCount);
end;                                                          

efg
________________________________________

MedTech Research Corporation
Lenexa, KS  66219  USA                    



Sun, 02 Apr 2000 03:00:00 GMT  
 Reading binary files in pascal

I am looking for a simple way to do it in <<standard pascal>>, so that
I can compile my source on both UNIX and Macintosh machines.

Davide



Sun, 02 Apr 2000 03:00:00 GMT  
 Reading binary files in pascal

Quote:
> I am looking for a simple way to do it in <<standard pascal>>, so that
> I can compile my source on both UNIX and Macintosh machines.

   Then I suggest you read the data from a "file of byte" and manipulate
the bytes you read in (1, 2, 10, whatever) as you need.  This will indeed
be complex, but working with blocks of bytes will do the I/o easily.


Sun, 02 Apr 2000 03:00:00 GMT  
 Reading binary files in pascal

Quote:

> I am looking for a simple way to do it in <<standard pascal>>, so that
> I can compile my source on both UNIX and Macintosh machines.

> Davide

Try comp.lang.pascal.ansi-iso
I'm not sure you can do this in standard pascal.

Jared



Sun, 02 Apr 2000 03:00:00 GMT  
 Reading binary files in pascal

Quote:


> > I am looking for a simple way to do it in <<standard pascal>>, so
> that
> > I can compile my source on both UNIX and Macintosh machines.

> > Davide

> Try comp.lang.pascal.ansi-iso
> I'm not sure you can do this in standard pascal.

     Sure you can.  What you need to do is to define the "base type" of
your file, which is the byte, and then declare the file as a file of
bytes.  What is important is to remember that
Pascal likes to work with constant sizes of things.  For example, in an
array, all of the
elements need to be of the same base type.  The same thing is true for
files, which can be thought of as open-ended "external" arrays.  [The
glaring exception is the file type "text",
which violates all of the carefully-crafted type rules of Pascal, but
makes for very friendly
i/o for the user].

    TYPE
      bytetype = 0 .. 255;
      filetype = PACKED FILE OF bytetype;

Note that not all compilers support the PACKED qualifier here.  However,
if you are working with a byte-oriented machine, and have defined
bytetype to fit into a byte, then
the implementation of "FILE OF bytetype" will probably also work.

     Now, of course, the responsibility for reading in as many bytes as
you want devolves back to you, the user.  You can only get one byte at a
time, but you can have a procedure
that returns two bytes, packed into a 16-bit word, or 4 bytes packed
into a real, or what
have you.

  FUNCTION readword : wordtype;

  { I assume wordtype has already been defined, perhaps as 0 .. 65535 }
  { I also assume, but this can vary depending on your machine's
"endian", that bytes
   are stored low, then high.  I also assume that the byte file is
"datafile" }

  TYPE
    twobytetype = RECORD
      CASE (b, w) OF
      b : (lowbyte, highbyte : bytetype);
      w : (word : wordtype) END;
  VAR
    twobyte : twobytetype;

   BEGIN   { readword }
    WITH twobyte DO
      BEGIN
       low := datafile^;   get (datafile);
       high := datafile^;  get (datafile);
       readword := word
      END
     END;

[ You have two chances to get the byte order mixed up (and I might have
done this here).
Once is when you read the data and assign it to low, then high, as
opposed to high, then
low.  The other is when you declare the variant record, and order the
bytes low, high or
high, low.  You may need to experiment a bit to get it right ...]

Bob Schor
Pascal Enthusiast



Mon, 03 Apr 2000 03:00:00 GMT  
 Reading binary files in pascal

: I am looking for a simple way to do it in <<standard pascal>>, so that
: I can compile my source on both UNIX and Macintosh machines.
Uh, standard pascal isn't really up to it I'm afraid, last time I did it,
some pretty weird constructs were needed.. buffers 'n multiple read()
statements included =]

OTOH, you can always get your hands on FPK-Pascal which will compile TP code
on Linux, or GNU Pascal for probably any other environment.

--
Bernhard van Staveren                   To reply, read the domain

"Linux, WinNT and MS-DOS. The Good, The Bad and The Ugly"



Wed, 05 Apr 2000 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Reading binary files in PASCAL ??

2. Can Excel read a Pascal binary file?

3. Converting binary (text file) to compress binary file

4. problems reading binary file, and outputting to screen.

5. reading binary file and outputting to standard output (screen)

6. I need to read a binary file

7. problem reading 4-byte reals from binary file

8. Reading Pascal-generated PACKED binary w/ C program

9. Making binary files in pascal for an OS

10. file2inc.zip Binary data to INC file maker for Turbo Pascal

11. Converting Turbo Pascal binary data files with C++

12. Binary -font file for Pascal

 

 
Powered by phpBB® Forum Software