[comp.lang.pascal] turbo & byte-reading

hiebeler@csv.rpi.edu (David Hiebeler) (08/11/87)

Hi there.  I am using Turbo-Pascal on an IBM PC and want to do some byte-
reading.  Specifically, here's the set-up:  I have some plotfiles.  These
files are stored simple as a series of bytes, each byte's bits representing
so many pixels (7 or 8?).  There is no header or anything in the plotfiles,
and they are stored one row after another, with no carriage returns in 
between or anything -- just a series of bytes.
  What I want to do is read in the file, one byte at a time, so that I may
process it, and construct an array of bits.  Does anyone know how to read
bytes like this in Turbo?  Assume the plot is PLANESIZE x PLANESIZE.  I can
handle converting the bytes into a 2-D array, I just can't get the bytes!
Thanks, please help me soon!
                             -D.H.
----
David Hiebeler       hiebeler@csv.rpi.edu
Chatham, NY         "Illusions, Richard!  Every
(also Troy, NY)      bit of it illusions!"

timothym@tekigm2.TEK.COM (Timothy D Margeson) (08/11/87)

Hi,
For most small applications, the following code should do the trick, however
if you need to read large amounts of data, a different approach is needed, 
using pointers to arrays of chars as row and column designators as an example.

============================== BEGIN FILEIO.PAS ================================

{$R+}{Enable array bounds checking}

const
  MaxChar = 2000; {this must be integer}
                  {there are tricks you must do if you need more than
                   32,768 characters}

type
  FileSpec  = string[13];
  TypeChar  = char;

var
  Error     : boolean;
  InChar    : array[1..MaxChar] of char;
  InFile    : file of char;
  InName    : FileSpec;
  CharCounter : real;
  I,J,K     : integer;

procedure Open1(InputFileName:FileSpec;var Error:boolean);
  begin
    Error:=false;
    assign(InFile,InputFileName);
    {$I-}reset(InFile){$I+};
    Error:=IOResult<>0;
  end;

procedure Usage;
  begin
    writeln('FILEIO  - A program to read a graphics file by character -');
    writeln('                                                          ');
    writeln('         Usage: fileio <file-to-read>                     ');
    writeln('                                                          ');
  end;

{================================ MAIN ======================================}

begin
  LowVideo;
  writeln;
  if ParamCount=0 then
    Usage
  else
    begin
      case Paramcount of
        1 : begin
              InName:=ParamStr(1);
            end;
      else
            begin
              writeln('Too many arguments in command line!');
              HALT
            end
      end; {case}

    begin
      for I:=1 to ord(InName[0]) do
        InName[I]:=upcase(InName[I]);
      Open1(InName,Error);
      if not Error then
        begin
          I:=1;
          while (not EOF(InFile)) and (I<MaxChar) do
            begin
              read(InFile,InChar[I]);
	      {write(InChar[I]); (* Leave this line in for tests with text *)}
            end;
          close(InFile);

        end; {if open1 error}
      end; {if else fill error}
    end; {if else param error}
end.


-- 
Tim Margeson (206)253-5240
PO Box 3500  d/s C1-937                          @@   'Who said that?'  
Vancouver, WA. 98668
{amd..hplabs}cae780!tektronix!tekigm2!timothym (this changes daily)