Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delphi: How do I save array to file?

490 views
Skip to first unread message

Martin Rommel

unread,
Jul 12, 1995, 3:00:00 AM7/12/95
to
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 /*

Steve Teixeira

unread,
Jul 12, 1995, 3:00:00 AM7/12/95
to
rom...@hermes.bc.edu (Martin Rommel) wrote:
>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.
>

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


Michael P. Hinds

unread,
Jul 13, 1995, 3:00:00 AM7/13/95
to
In article <rommel-1207...@phstudent1.bc.edu> rom...@hermes.bc.edu (Martin Rommel) writes:
>From: rom...@hermes.bc.edu (Martin Rommel)
>Subject: Delphi: How do I save array to file?
>Date: Wed, 12 Jul 1995 00:26:38 -0400

>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.

Simon Oke

unread,
Jul 13, 1995, 3:00:00 AM7/13/95
to
In article <mikehinds....@mikehinds.seanet.com>

mike...@mikehinds.seanet.com "Michael P. Hinds" writes:

>
> 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

Dag Sunde

unread,
Jul 13, 1995, 3:00:00 AM7/13/95
to

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.


van...@wkuvx1.wku.edu

unread,
Jul 18, 1995, 3:00:00 AM7/18/95
to
In article <805659...@loony.demon.co.uk>, Simon Oke <si...@loony.demon.co.uk> writes:
> In article <mikehinds....@mikehinds.seanet.com>
> mike...@mikehinds.seanet.com "Michael P. Hinds" writes:
>
>>
>> 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 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


0 new messages