• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Saving to file

I need to save data stored in some arrays in a binary file. Then after, a need to load from the file and store it again in the arrays.

How I can do that?



0
scoopbh
Asked:
scoopbh
1 Solution
 
DaFoxCommented:
Hi.

I would use streams (TFileStream).
Maybe this piece of code helps (untested!!):

type
  MyArray : array[0..?] of ?;

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;

HTH and HTW (hope this works ;-)

Markus
0
 
billiousCommented:
Scant details - are the arrays all of the same size, do you need to read different sets of arrays, etc.

Essentially, declare

type
  arraytype : array [start..finish] of type;

var
  arrayfile : file of arraytype;
  v1        : arraytype;

beyond that,

  assignfile(arrayfile,'filename');
  reset(arrayfile);
  read(arrayfile,v1);
  closefile(arrayfile);
 
  will read the first record from the file to v1

  assignfile(arrayfile,'filename');
  reset(arrayfile);
  seek(arrayfile,12);
  read(arrayfile,v1);
  closefile(arrayfile);

  will read the twelfth record from the file to v1

  assignfile(arrayfile,'filename');
  reset(arrayfile);
  seek(arrayfile,12);
  write(arrayfile,v1);
  closefile(arrayfile);

  will write v1 as the 12th record of arrayfile

  assignfile(arrayfile,'filename');
  rewrite(arrayfile);
  write(arrayfile,v1);
  closefile(arrayfile);

  will write v1 as the only record of arrayfile

...really depends on quite what you want to do.

Look for the reset, rewrite, seek HELP.

   obviously, you can also open the file once (assignfile, reset) at the beginning of your process, and close it at the end. As I say, depends on the detail what you want to
do.

HTH

Bill


 


0
 
scoopbhAuthor Commented:
Hi, I think this work well. And if instead of a array a wish to save a object, with the structure and the data.

Like this:

type
    component = class
    name, place: string;
    age: integer;
  end;

 type
  team = class
  name: string;
  components: array[1..25] of ;
  end;

if a create a objetc, store some data in it variables. How I save the structure and data of this created object?
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
CleanupPingCommented:
scoopbh:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
kacorretiredCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       PAQ and refund points
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Merry Christmas and happy new year!

kacor
EE Cleanup Volunteer
0
 
SpazMODicCommented:
PAQed, with points refunded (20)

SpazMODic
EE Moderator
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now