?
Solved

Saving to file

Posted on 2003-02-28
6
Medium Priority
?
188 Views
Last Modified: 2010-04-04
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
Comment
Question by:scoopbh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:DaFox
ID: 8043127
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
 
LVL 7

Expert Comment

by:billious
ID: 8043260
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
 

Author Comment

by:scoopbh
ID: 8043433
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:CleanupPing
ID: 9317017
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
 
LVL 10

Expert Comment

by:kacor
ID: 9992668
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
 

Accepted Solution

by:
SpazMODic earned 0 total points
ID: 10015290
PAQed, with points refunded (20)

SpazMODic
EE Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question