Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic record-how to?

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
I'm trying to read a byte files. I can find out the length of the file with the filesize command.
Here's the code:
TCurveLoad = record
name:array[1..110] of byte;

end;

TCurveLoadFile = file of TCurveLoad;

The 110 value is the size in bytes of the file. To write the file, it's no problem but to read it, how do i make it name:array[1..66] of bytes if the file is only 66 bytes.

I hope i'm making sense.
Thanks.
PO
 
use dynamic arrays for this purpose.

here's an example with TFilestream (my favorite)

Code:
uses SysUtils,...

type TByteArray = array of Byte;
...
//!!! untested
function readfile(Filename : string; NumberofBytesToRead : integer) : TByteArray;

var FileStream : TFileStream;
    
begin
 FileStream := TFileStream.Create(filename, fmOpenRead or fmShareDenyNone); 
 try
  SetLength(Result, NumberofBytesToRead);
  // read our bytes
  FileStream.Position := 0; 
  FileStream.Read(Result[0], NumberOfBytesToRead); 
 finally
  FreeAndNil(FileStream);
 end;
end;

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks, i wrote the code and got no error. Now i have the function readfile, how do i implement it? sorry, that may be a stupid question.
Sincerely,
PO
 
euh....

Code:
procedure DaProcedure;

var CurveLoad : array of Byte;
    Index : Integer;
    DaByte : Byte;

begin
 CurveLoad := readfile('afilename', 66);
 for Index := Low(Curveload) to High(CurveLoad) do
  begin
   DaByte := CurveLoad[Index];
   DoSomethingwithDaByte(DaByte);
  end;
end;

clear enough???

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Kind of: I get an incompatibe type "dynamic array' and 'tbytearray' at the line curveload:=readfile('afilename',66);

is that easy to fix? thanks for all your help.
PO
 
oh boy

Code:
procedure DaProcedure;

var CurveLoad : TByteArray;
    Index : Integer;
    DaByte : Byte;

begin
 CurveLoad := readfile('afilename', 66);
 for Index := Low(Curveload) to High(CurveLoad) do
  begin
   DaByte := CurveLoad[Index];
   DoSomethingwithDaByte(DaByte);
  end;
end;

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Works like a charm. You are Da man with Da procedure
Thanks a million. Now i know on how to use dynamic record...

PO
 
Let me ask you one more question if i may. In my normal record setup i use:
type
Tcurve = record
name:array[1..110] of byte;
end;

TcurveFile = file of Tcurve;
var
h:tcurvefile;
Rec2:tcurve;

To write to the file, i would do write(h,rec2);

Now that i use tbytearray, how do i write to a file?

Thanks again..
PO
 
check the TFilestream manual in delphi,

use
TFileStream.Create(filename, fmCreate or fmShareDenyNone);
if the file does not exist (check with FileExists function) yet or else use
FileStream := TFileStream.Create(filename, fmOpenRead or fmShareDenyNone);

use the write method to write your bytes.

Is this a school assigment? try to figure it out yourself this time, with the code I gave you, this should be no problem...

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I wish it was a school assignment and i was still in college. I'm a little bit old for that. I used to program in Turbo Pascal when I was in College 20 years ago. I'm confused about Delphi. It's not the old DOS thing I used to use.
I'll dig in the manual.
Thanks for all your help.
PO
 
The same methods under turbo pascal still work under delphi. The only differences (that I can think of at the moment) are:
Assign(f, fn) is AssignFile(f, fn) under Delphi and
Close(f) is CloseFile(f) under Delphi.

Demonstration example:
Code:
unit BlockTestUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    WriteBtn: TButton;
    ReadBtn: TButton;
    Memo1: TMemo;
    procedure WriteBtnClick(Sender: TObject);
    procedure ReadBtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  a: array[1..110] of byte;
  f: file of byte;
const
  test = 'test.fil';

procedure TForm1.WriteBtnClick(Sender: TObject);
var
  i, numwritten: integer;
begin
  fillchar(a, sizeof(a), #0);  //zero out the array
  for i:= 1 to 66 do //66 of 110
    a[i]:= byte(i);  //arbitrary values for test purposes
  assignfile(f, test);
  rewrite(f);
  blockwrite(f, a, sizeof(a), numwritten);
  CloseFile(f);
  showMessage('[' + inttostr(numwritten) + '] bytes written to file "' + test + '".')
end;

procedure TForm1.ReadBtnClick(Sender: TObject);
var
  i, numread: integer;
begin
  memo1.Lines.Clear;
  fillchar(a, sizeof(a), #0); //zero out the array
  assignfile(f, test);
  reset(f);
  i:= 0;
  repeat
    inc(i);
    blockread(f, a[i], 1, numread);
    memo1.Lines.Add(inttostr(a[i]));
  until eof(f) or (numread = 0);
  CloseFile(f);
end;

end.
(tested code - works for me under D7E)

I've successfully pasted many TP apps directly into delphi and they ran with minor modification using compiler directive {$APPTYPE CONSOLE}, particularly if you have a lot of readln() and writeln() statements. In fact, I still do preliminary coding as console apps because you can still see your output on a break-point, unlike a GUI app. Console apps also run about a hundred times faster without the GUI.

Welcome to our forum!

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top