\\\\ ////
C-@,@ J. Martin Rommel (rom...@hermes.bc.edu) @/@-o
"\ -/ \o /*
How about something like this (this is off the top o' my head, so double check
it):
type
MyArray : array[1..127] of word;
procedure SaveArray(var A: MyArray);
var
S: TFileStream;
begin
S := TFileStream.Create('ARRAY.SAV', fmCreate);
try
S.Write(A, SizeOf(A));
finally
S.Free;
end;
end;
procedure LoadArray(var A: MyArray);
var
S: TFileStream;
begin
S := TFileStream.Create('ARRAY.SAV', fmOpenRead);
try
S.Read(A, SizeOf(A));
finally
S.Free;
end;
end;
-Steve Teixeira
stei...@borland.com
>I have an array on my form which I like to save and load from disk. Is
>there a quick and easy way or do I have to do some workaround like
>converting it into a text file (and use .Lines.SaveToFile) or create an
>object out of it?
>I'd like to avoid using the DB unit (I have not looked into the whole
>database business, and hope I don't have to).
>Any hints appreciated.
>\\\\ ////
>C-@,@ J. Martin Rommel (rom...@hermes.bc.edu) @/@-o
>"\ -/ \o /*
>
If you have defined a type for your array, it is a cinch:
program myProg;
type
myArray = ARRAY[1..20] OF MyRec;
var f : File of myArray;
A : MyArray;
begin
Assign(F, "filename.ext");
Reset(F);
{ and I believe I've read that assign and reset may have changed somewhat }
Read( F, A );
{ do stuff with A here }
Write( F, A );
Close( F );
end.
>
> If you have defined a type for your array, it is a cinch:
>
[ stuff deleted ]...
OK, here's one that's been bugging me for a while.
I have a file which contains a contiguous block of (same-length)
records. I can only determine the number of records in the file
by opening it and reading a header from it. How can I read the records
in and access them as an array? Currently I have to use AllocMem
or whatever, and have a function which munges pointers to get the
address of the particular numbered record I want.
[BTW, this refers to the problem of reading the directory from
a DOOM .WAD...]
{ flame-proof trousers on }
Am I missing some obvious way of doing this, or is C superior in
this respect?
--
Simon Oke
If I'm not totally off the point, this should do it:
TYPE
TMyRec = Array[1..RecordSize] OF Byte;
VAR
f : File OF TMyRec;
MyRec : Array[1..NumRec] OF TMyRec;
n : WORD;
BEGIN
Assign( f, 'Jalla.WAD' );
ReSet( f, Length(MyRec) );
n := 0;
WHILE NOT EOF( f ) DO
BEGIN
Inc(n);
Read( f, MyRec[n] );
END; { while }
Close( f );
End;
Dag Sunde.
If you can assume that the total will be less thean 64k it's pretty
easy, otherwise you'll have to do a bit of tweaking such as having
array of pointers, etc... here goes.
type
fooRec = record
{ Your record def here }
end;
recArray = array[0..0] of fooRec;
ptrRecArray = ^recArray;
var
recs : ptrRecArray;
begin
:
{ Make sure range checking is off }
{ Read in # of records to allocate }
x := numRecs;
GetMem(recs, x * sizeof(fooRec));
BlockRead(file, recs^, x * sizeof(fooRec));
Now, you can access the records via recs[0]...recs[n]. This is the
simplist way I can find. Make sure you have range checking off during
access or you will get a runtime error.
Jay Cole