Solved

saving arrays to dat

Posted on 2004-08-19
4
264 Views
Last Modified: 2010-04-05
hey all :)
I need advice, I have been looking at several ways to save arrays of records to a file and I was wondering whats the best way.
I need to save several arrays of records ( static & dynamic), and the hard part is that dynamic arrays can have varying lengths,
and most of them will exceed 255 characters. Which means I cant use "file of TRecord" to save the records. And I want to
save all my array records (6-8 of them) to one file.

Now I have looked into TFileStream and writing the individual records out manually. And its ok, but tedious.
And TStringLists, which I can use... but how do I save them out in a non text format? I dont want users to be able to
read (or change) anything in the file. Anyway to change that?

thanx for your time...



0
Comment
Question by:LMuadDIb
4 Comments
 
LVL 33

Accepted Solution

by:
Slick812 earned 250 total points
Comment Utility
hello  LMuadDIb, , I have used the delphi TFileStream, many, many times to save all kinds of  data into a file, if you have an array of records, you can for loop through the array and write each record to the TFileStream, but you will need to store the "Length" of the array as a file data segment, so you can read the file later and know how many records to read out of it. . . , if you used NON-FIXED size variables  (like a  String) then you will need a diferent method to get in in and out of a file data segment, you would record the length of the string (usually an integer value) and then write all of the characters in the string to file. . . . I have made a web site for using the TfileStream for muti-segment data files at --

http://www.angelfire.com/hi5/delphizeus/customfiles.html
0
 
LVL 2

Assisted Solution

by:DarkCore_
DarkCore_ earned 50 total points
Comment Utility
Or just you can try instead of saving in a bulk file, save into a XML file. Maybe more tedious ... but a solution at all.

Edu
0
 
LVL 3

Assisted Solution

by:lmikle
lmikle earned 50 total points
Comment Utility
unit Unit2;

interface

uses Windows, SysUtils, Classes;

type
  TRec1 = record
    I : Integer;
    S : String;
  end;

  TRec2 = record
    I : Integer;
    B : Boolean;
  end;

procedure SaveToFile(FileName : String);
procedure LoadFromStream(FileName : String);

var
   Rec1 : Array [1..10] Of TRec1; // Static array
   Rec2 : Array of TRec2; // Dynamic array

implementation

procedure SaveToFile(FileName : String);
var
  I : Integer;
  FS : TFileStream;
  sSize : Integer;
  sData : String;
begin
  // Open file to write (create)
  FS := TFileStream.Create(FileName,fmCreate);

  // Save static array - we know numbers of items
  For I := 1 To 10 Do
    begin
      // Write integer
       FS.WriteBuffer(Rec1[I].I,SizeOf(Integer));

       // Write String - write length first, then write data
       sSize := Length(Rec1[I].S);
       sData := Rec1[I].S;
       FS.WriteBuffer(sSize,SizeOf(Integer));
       FS.WriteBuffer(sData[1],sSize);
    end;

  // Save dynamic array
  // Write number of items first
  sSize := Length(Rec2);
  FS.WriteBuffer(sSize,SizeOf(Integer));
  For I := Low(Rec2) To High(Rec2) Do
    begin
      FS.WriteBuffer(Rec2[I].I,SizeOf(Integer));
      FS.WriteBuffer(Rec2[I].B,SizeOf(Boolean));
    end;

  FS.Free;
end;

procedure LoadFromStream(FileName : String);
var
  I : Integer;
  FS : TFileStream;
  sSize : Integer;
  sData : String;
begin
  // Open file to read
  FS := TFileStream.Create(FileName,fmOpenRead);

  // read static array
  For I := 1 To 10 Do
    Begin
      // read integer
      FS.ReadBuffer(Rec1[I].I,SizeOf(Integer));

      // read string
      FS.ReadBuffer(sSize,SizeOf(Integer));
      SetLength(sData,sSize);
      FS.ReadBuffer(sData[1],sSize);
      Rec1[I].S := sData;
    End;

  // read dynamic array
  FS.ReadBuffer(sSize,SizeOf(Integer));
  SetLength(Rec2,sSize);
  For I := Low(Rec2) To High(Rec2) Do
    begin
      FS.ReadBuffer(Rec2[I].I,SizeOf(Integer));
      FS.ReadBuffer(Rec2[I].B,SizeOf(Boolean));
    end;

  FS.Free;
end;

end.
0
 
LVL 4

Author Comment

by:LMuadDIb
Comment Utility
Slick812 thanx for that link :)
 DarkCore, I didnt even think about xml (which I have never used) but Im gonna check that out thanx
and  lmikle, thanx for the code
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IExtractImage Delphi 14 162
ddeman not working in activex 3 78
Intraweb download file link ? 1 87
Convert a string into a TDateTime 5 44
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now